





'''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							:     int   )					->       List[Any]:







														"""simple docstring"""
														UpperCAmelCase_			:      Optional[Any]     						=  []

							def     UpperCAmelCase__	(							self							:     List[Any]       ,     __magic_name__							:     int       ,     __magic_name__							:     List[str]       ,     __magic_name__							:     str       ,     **__magic_name__							:     Optional[int]   )					->       Union[str, Any]:







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

							def     UpperCAmelCase__	(							self							:     Union[str, Any]       ,     __magic_name__							:     List[str]       ,     __magic_name__							:     str       ,     __magic_name__							:     Dict       ,     **__magic_name__							:     Tuple   )					->       str:







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

							def     UpperCAmelCase__	(							self							:     Dict       ,     __magic_name__							:     Optional[Any]       ,     __magic_name__							:     Union[str, Any]       ,     __magic_name__							:     Union[str, Any]       ,     **__magic_name__							:     Dict   )					->       Optional[Any]:







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

							def     UpperCAmelCase__	(							self							:     Tuple       ,     __magic_name__							:     str       ,     __magic_name__							:     Any       ,     __magic_name__							:     int       ,     **__magic_name__							:     Tuple   )					->       str:







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

							def     UpperCAmelCase__	(							self							:     Any       ,     __magic_name__							:     Optional[int]       ,     __magic_name__							:     str       ,     __magic_name__							:     Union[str, Any]       ,     **__magic_name__							:     str   )					->       List[Any]:







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

							def     UpperCAmelCase__	(							self							:     Optional[Any]       ,     __magic_name__							:     Any       ,     __magic_name__							:     Union[str, Any]       ,     __magic_name__							:     str       ,     **__magic_name__							:     Dict   )					->       Optional[int]:







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

							def     UpperCAmelCase__	(							self							:     Union[str, Any]       ,     __magic_name__							:     Optional[Any]       ,     __magic_name__							:     str       ,     __magic_name__							:     Any       ,     **__magic_name__							:     Any   )					->       Union[str, Any]:







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

							def     UpperCAmelCase__	(							self							:     Optional[int]       ,     __magic_name__							:     Any       ,     __magic_name__							:     Union[str, Any]       ,     __magic_name__							:     List[Any]       ,     **__magic_name__							:     str   )					->       int:







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

							def     UpperCAmelCase__	(							self							:     int       ,     __magic_name__							:     Tuple       ,     __magic_name__							:     Dict       ,     __magic_name__							:     str       ,     **__magic_name__							:     Tuple   )					->       List[Any]:







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

							def     UpperCAmelCase__	(							self							:     Optional[Any]       ,     __magic_name__							:     List[Any]       ,     __magic_name__							:     Any       ,     __magic_name__							:     List[Any]       ,     **__magic_name__							:     Optional[Any]   )					->       Union[str, Any]:







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

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







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



							def     UpperCAmelCase__	(							self							:     Any       ,     __magic_name__							:     Optional[int]       ,     __magic_name__							:     List[str]       ,     __magic_name__							:     Dict       ,     **__magic_name__							:     List[Any]   )					->       List[Any]:







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





@require_torch
class 					__a      (unittest.TestCase     ):

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







														"""simple docstring"""
														UpperCAmelCase_			:      str     						=  tempfile.mkdtemp()

							def     UpperCAmelCase__	(							self							:     Tuple   )					->       int:







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

							def     UpperCAmelCase__	(							self							:     Union[str, Any]       ,     __magic_name__							:     List[str]=0       ,     __magic_name__							:     Union[str, Any]=0       ,     __magic_name__							:     str=64       ,     __magic_name__							:     List[str]=64       ,     __magic_name__							:     List[str]=None       ,     __magic_name__							:     Any=False       ,     **__magic_name__							:     Optional[Any]   )					->       str:







														"""simple docstring"""
														UpperCAmelCase_			:      Tuple     						=  RegressionDataset(length=_a   )
														UpperCAmelCase_			:      List[Any]     						=  RegressionDataset(length=_a   )
														UpperCAmelCase_			:      List[str]     						=  RegressionModelConfig(a=_a       ,     b=_a   )
														UpperCAmelCase_			:      Union[str, Any]     						=  RegressionPreTrainedModel(_a   )

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

							def     UpperCAmelCase__	(							self							:     Any       ,     __magic_name__							:     Dict       ,     __magic_name__							:     Union[str, Any]   )					->       int:







														"""simple docstring"""
														self.assertEqual(len(_a   )       ,     len(_a   )   )

														# Order doesn't matter
														UpperCAmelCase_			:      List[str]     						=  sorted(_a       ,     key=lambda __magic_name__   : cb.__name__ if isinstance(_a       ,     _a   ) else cb.__class__.__name__   )
														UpperCAmelCase_			:      List[Any]     						=  sorted(_a       ,     key=lambda __magic_name__   : 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     UpperCAmelCase__	(							self							:     Optional[Any]       ,     __magic_name__							:     Union[str, Any]   )					->       List[str]:







														"""simple docstring"""
														UpperCAmelCase_			:      Optional[Any]     						=  ["""on_init_end""", """on_train_begin"""]
														UpperCAmelCase_			:      Optional[int]     						=  0
														UpperCAmelCase_			:      List[Any]     						=  len(trainer.get_eval_dataloader()   )
														UpperCAmelCase_			:      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     UpperCAmelCase__	(							self							:     List[Any]   )					->       Optional[Any]:







														"""simple docstring"""
														UpperCAmelCase_			:      Tuple     						=  self.get_trainer()
														UpperCAmelCase_			:      Union[str, Any]     						=  DEFAULT_CALLBACKS.copy() + [ProgressCallback]
														self.check_callbacks_equality(trainer.callback_handler.callbacks       ,     _a   )

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

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







														"""simple docstring"""
														UpperCAmelCase_			:      str     						=  DEFAULT_CALLBACKS.copy() + [ProgressCallback]
														UpperCAmelCase_			:      Any     						=  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   )

														UpperCAmelCase_			:      Optional[int]     						=  self.get_trainer()
														UpperCAmelCase_			:      List[Any]     						=  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
														UpperCAmelCase_			:      Optional[Any]     						=  self.get_trainer()
														UpperCAmelCase_			:      Optional[int]     						=  trainer.callback_handler.callbacks[0]
														trainer.remove_callback(_a   )
														expected_callbacks.remove(_a   )
														self.check_callbacks_equality(trainer.callback_handler.callbacks       ,     _a   )

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







														"""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=_a   )

														UpperCAmelCase_			:      Dict     						=  self.get_trainer(callbacks=[MyTestTrainerCallback]   )
														trainer.train()
														UpperCAmelCase_			:      str     						=  trainer.callback_handler.callbacks[-2].events
														self.assertEqual(_a       ,     self.get_expected_events(_a   )   )

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

														UpperCAmelCase_			:      Any     						=  self.get_trainer(callbacks=[MyTestTrainerCallback]       ,     save_steps=5   )
														trainer.train()
														UpperCAmelCase_			:      Tuple     						=  trainer.callback_handler.callbacks[-2].events
														self.assertEqual(_a       ,     self.get_expected_events(_a   )   )

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

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

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












'''simple docstring'''







from collections.abc import Iterable
from typing import Any


class 					__a      :

							def __init__(							self							:     Optional[Any]       ,     __magic_name__							:     int | None = None   )					->       Tuple:







													"""simple docstring"""
													UpperCAmelCase_			:      List[str]     						=  value
													UpperCAmelCase_			:      Node | None     						=  None  # Added in order to delete a node easier
													UpperCAmelCase_			:      Node | None     						=  None
													UpperCAmelCase_			:      Node | None     						=  None



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







													"""simple docstring"""
													from pprint import pformat

													if self.left is None and self.right is None:
																			return str(self.value   )
													return pformat({F"""{self.value}""": (self.left, self.right)}       ,     indent=1   )





class 					__a      :

							def __init__(							self							:     int       ,     __magic_name__							:     Node | None = None   )					->       Optional[int]:







													"""simple docstring"""
													UpperCAmelCase_			:      str     						=  root

							def __str__(							self							:     Any   )					->       str:







													"""simple docstring"""
													return str(self.root   )

							def     UpperCAmelCase__	(							self							:     Any       ,     __magic_name__							:     Node       ,     __magic_name__							:     Node | None   )					->       None:







													"""simple docstring"""
													if new_children is not None:  # reset its kids
																			UpperCAmelCase_			:      Dict     						=  node.parent
													if node.parent is not None:  # reset its parent
																			if self.is_right(__magic_name__   ):  # If it is the right children
																									UpperCAmelCase_			:      Optional[Any]     						=  new_children
																			else:
																									UpperCAmelCase_			:      Optional[int]     						=  new_children
													else:
																			UpperCAmelCase_			:      List[str]     						=  new_children

							def     UpperCAmelCase__	(							self							:     List[str]       ,     __magic_name__							:     Node   )					->       bool:







													"""simple docstring"""
													if node.parent and node.parent.right:
																			return node == node.parent.right
													return False

							def     UpperCAmelCase__	(							self							:     Union[str, Any]   )					->       bool:







													"""simple docstring"""
													return self.root is None

							def     UpperCAmelCase__	(							self							:     Any       ,     __magic_name__							:     str   )					->       None:







													"""simple docstring"""
													UpperCAmelCase_			:      Tuple     						=  Node(__magic_name__   )  # create a new Node
													if self.empty():  # if Tree is empty
																			UpperCAmelCase_			:      List[Any]     						=  new_node  # set its root
													else:  # Tree is not empty
																			UpperCAmelCase_			:      str     						=  self.root  # from root
																			if parent_node is None:
																									return
																			while True:  # While we don't get to a leaf
																									if value < parent_node.value:  # We go left
																															if parent_node.left is None:
																																					UpperCAmelCase_			:      Union[str, Any]     						=  new_node  # We insert the new node in a leaf
																																					break
																															else:
																																					UpperCAmelCase_			:      List[Any]     						=  parent_node.left
																									else:
																															if parent_node.right is None:
																																					UpperCAmelCase_			:      List[Any]     						=  new_node
																																					break
																															else:
																																					UpperCAmelCase_			:      Union[str, Any]     						=  parent_node.right
																			UpperCAmelCase_			:      Union[str, Any]     						=  parent_node

							def     UpperCAmelCase__	(							self							:     Optional[Any]       ,     *__magic_name__							:     List[str]   )					->       None:







													"""simple docstring"""
													for value in values:
																			self.__insert(__magic_name__   )

							def     UpperCAmelCase__	(							self							:     Dict       ,     __magic_name__							:     int   )					->       Node | None:







													"""simple docstring"""
													if self.empty():
																			raise IndexError('''Warning: Tree is empty! please use another.'''   )
													else:
																			UpperCAmelCase_			:      str     						=  self.root
																			# use lazy evaluation here to avoid NoneType Attribute error
																			while node is not None and node.value is not value:
																									UpperCAmelCase_			:      List[str]     						=  node.left if value < node.value else node.right
																			return node

							def     UpperCAmelCase__	(							self							:     Optional[int]       ,     __magic_name__							:     Node | None = None   )					->       Node | None:







													"""simple docstring"""
													if node is None:
																			if self.root is None:
																									return None
																			UpperCAmelCase_			:      Dict     						=  self.root

													if not self.empty():
																			while node.right is not None:
																									UpperCAmelCase_			:      Any     						=  node.right
													return node

							def     UpperCAmelCase__	(							self							:     Dict       ,     __magic_name__							:     Node | None = None   )					->       Node | None:







													"""simple docstring"""
													if node is None:
																			UpperCAmelCase_			:      Optional[int]     						=  self.root
													if self.root is None:
																			return None
													if not self.empty():
																			UpperCAmelCase_			:      Union[str, Any]     						=  self.root
																			while node.left is not None:
																									UpperCAmelCase_			:      Dict     						=  node.left
													return node

							def     UpperCAmelCase__	(							self							:     Tuple       ,     __magic_name__							:     int   )					->       None:







													"""simple docstring"""
													UpperCAmelCase_			:      List[str]     						=  self.search(__magic_name__   )  # Look for the node with that label
													if node is not None:
																			if node.left is None and node.right is None:  # If it has no children
																									self.__reassign_nodes(__magic_name__       ,     __magic_name__   )
																			elif node.left is None:  # Has only right children
																									self.__reassign_nodes(__magic_name__       ,     node.right   )
																			elif node.right is None:  # Has only left children
																									self.__reassign_nodes(__magic_name__       ,     node.left   )
																			else:
																									UpperCAmelCase_			:      List[str]     						=  self.get_max(
																									    node.left   )  # Gets the max value of the left branch
																									self.remove(tmp_node.value   )  # type: ignore
																									UpperCAmelCase_			:      Optional[int]     						=  (
																									    tmp_node.value  # type: ignore
																									)  # Assigns the value to the node to delete and keep tree structure

							def     UpperCAmelCase__	(							self							:     List[Any]       ,     __magic_name__							:     Node | None   )					->       Iterable:







													"""simple docstring"""
													if node is not None:
																			yield node  # Preorder Traversal
																			yield from self.preorder_traverse(node.left   )
																			yield from self.preorder_traverse(node.right   )

							def     UpperCAmelCase__	(							self							:     List[Any]       ,     __magic_name__							:     List[Any]=None   )					->       Any:







													"""simple docstring"""
													if traversal_function is None:
																			return self.preorder_traverse(self.root   )
													else:
																			return traversal_function(self.root   )

							def     UpperCAmelCase__	(							self							:     Optional[int]       ,     __magic_name__							:     list       ,     __magic_name__							:     Node | None   )					->       None:







													"""simple docstring"""
													if node:
																			self.inorder(__magic_name__       ,     node.left   )
																			arr.append(node.value   )
																			self.inorder(__magic_name__       ,     node.right   )



							def     UpperCAmelCase__	(							self							:     Tuple       ,     __magic_name__							:     int       ,     __magic_name__							:     Node   )					->       int:







													"""simple docstring"""
													UpperCAmelCase_			:      list[int]     						=  []
													self.inorder(__magic_name__       ,     __magic_name__   )  # append all values to list using inorder traversal
													return arr[k - 1]





def        lowerCamelCase_      (  SCREAMING_SNAKE_CASE__      :			Node | None   ) ->	list[Node]:
						UpperCAmelCase_			:      Any     						=  []
						if curr_node is not None:
												UpperCAmelCase_			:      Any     						=  postorder(curr_node.left   ) + postorder(curr_node.right   ) + [curr_node]
						return node_list





def        lowerCamelCase_      (  ) ->	None:
						UpperCAmelCase_			:      str     						=  (8, 3, 6, 1, 10, 14, 13, 4, 7)
						UpperCAmelCase_			:      Tuple     						=  BinarySearchTree()
						for i in testlist:
												t.insert(SCREAMING_SNAKE_CASE__   )

						# Prints all the elements of the list in order traversal
						print(SCREAMING_SNAKE_CASE__   )

						if t.search(6   ) is not None:
												print('''The value 6 exists'''   )
						else:
												print('''The value 6 doesn\'t exist'''   )

						if t.search(-1   ) is not None:
												print('''The value -1 exists'''   )
						else:
												print('''The value -1 doesn\'t exist'''   )

						if not t.empty():
												print('''Max Value: ''',			t.get_max().value   )  # type: ignore
												print('''Min Value: ''',			t.get_min().value   )  # type: ignore

						for i in testlist:
												t.remove(SCREAMING_SNAKE_CASE__   )
												print(SCREAMING_SNAKE_CASE__   )


if __name__ == "__main__":
					import doctest

					doctest.testmod(verbose=True)





