



'''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_		(     snake_case    ):


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


							def    UpperCamelCase     (				self   :						List[Any]      ,						a_   :						List[str]      ,						a_   :						Optional[Any]      ,						a_   :						Optional[Any]      ,						**a_   :						Dict			)			->					Dict:
										self.events.append('on_init_end'			)


							def    UpperCamelCase     (				self   :						Optional[int]      ,						a_   :						List[str]      ,						a_   :						Tuple      ,						a_   :						Dict      ,						**a_   :						Optional[int]			)			->					str:
										self.events.append('on_train_begin'			)


							def    UpperCamelCase     (				self   :						Optional[int]      ,						a_   :						Any      ,						a_   :						Optional[Any]      ,						a_   :						List[Any]      ,						**a_   :						List[str]			)			->					List[Any]:
										self.events.append('on_train_end'			)


							def    UpperCamelCase     (				self   :						Optional[int]      ,						a_   :						Tuple      ,						a_   :						Any      ,						a_   :						Tuple      ,						**a_   :						Tuple			)			->					int:
										self.events.append('on_epoch_begin'			)


							def    UpperCamelCase     (				self   :						List[Any]      ,						a_   :						Union[str, Any]      ,						a_   :						Optional[int]      ,						a_   :						List[str]      ,						**a_   :						Union[str, Any]			)			->					Tuple:
										self.events.append('on_epoch_end'			)


							def    UpperCamelCase     (				self   :						List[str]      ,						a_   :						List[Any]      ,						a_   :						Tuple      ,						a_   :						str      ,						**a_   :						int			)			->					List[str]:
										self.events.append('on_step_begin'			)


							def    UpperCamelCase     (				self   :						List[Any]      ,						a_   :						Any      ,						a_   :						Tuple      ,						a_   :						Tuple      ,						**a_   :						Any			)			->					Dict:
										self.events.append('on_step_end'			)


							def    UpperCamelCase     (				self   :						Union[str, Any]      ,						a_   :						Tuple      ,						a_   :						Optional[int]      ,						a_   :						Any      ,						**a_   :						Tuple			)			->					List[Any]:
										self.events.append('on_evaluate'			)


							def    UpperCamelCase     (				self   :						int      ,						a_   :						Dict      ,						a_   :						int      ,						a_   :						int      ,						**a_   :						List[Any]			)			->					List[Any]:
										self.events.append('on_predict'			)


							def    UpperCamelCase     (				self   :						str      ,						a_   :						List[str]      ,						a_   :						str      ,						a_   :						Optional[Any]      ,						**a_   :						Dict			)			->					List[str]:
										self.events.append('on_save'			)


							def    UpperCamelCase     (				self   :						Union[str, Any]      ,						a_   :						Any      ,						a_   :						Union[str, Any]      ,						a_   :						Dict      ,						**a_   :						Union[str, Any]			)			->					Optional[int]:
										self.events.append('on_log'			)




							def    UpperCamelCase     (				self   :						Any      ,						a_   :						Tuple      ,						a_   :						Dict      ,						a_   :						str      ,						**a_   :						str			)			->					str:
										self.events.append('on_prediction_step'			)






@require_torch
class 						a_		(     unittest.TestCase    ):


							def    UpperCamelCase     (				self   :						int			)			->					Union[str, Any]:
										snake_case:    List[str]      							=tempfile.mkdtemp()


							def    UpperCamelCase     (				self   :						List[str]			)			->					Tuple:
										shutil.rmtree(self.output_dir			)


							def    UpperCamelCase     (				self   :						Optional[Any]      ,						a_   :						Tuple=0      ,						a_   :						List[str]=0      ,						a_   :						Optional[Any]=6_4      ,						a_   :						List[str]=6_4      ,						a_   :						int=None      ,						a_   :						Optional[int]=False      ,						**a_   :						List[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.
										snake_case:    List[str]      							=RegressionDataset(length=a_			)
										snake_case:    Union[str, Any]      							=RegressionDataset(length=a_			)
										snake_case:    Dict      							=RegressionModelConfig(a=a_      ,						b=a_			)
										snake_case:    Optional[int]      							=RegressionPreTrainedModel(a_			)

										snake_case:    Dict      							=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   :						str      ,						a_   :						List[Any]      ,						a_   :						Optional[Any]			)			->					int:
										self.assertEqual(len(a_			)      ,						len(a_			)			)

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

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


							def    UpperCamelCase     (				self   :						Dict			)			->					Tuple:
										snake_case:    Tuple      							=DEFAULT_CALLBACKS.copy() + [ProgressCallback]
										snake_case:    int      							=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_			)

										snake_case:    str      							=self.get_trainer()
										snake_case:    int      							=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
										snake_case:    Tuple      							=self.get_trainer()
										snake_case:    List[str]      							=trainer.callback_handler.callbacks[0]
										trainer.remove_callback(a_			)
										expected_callbacks.remove(a_			)
										self.check_callbacks_equality(trainer.callback_handler.callbacks      ,						a_			)

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

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

										# Independent log/save/eval
										snake_case:    Union[str, Any]      							=self.get_trainer(callbacks=[MyTestTrainerCallback]      ,						logging_steps=5			)
										trainer.train()
										snake_case:    Any      							=trainer.callback_handler.callbacks[-2].events
										self.assertEqual(a_      ,						self.get_expected_events(a_			)			)

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

										snake_case:    str      							=self.get_trainer(callbacks=[MyTestTrainerCallback]      ,						eval_steps=5      ,						evaluation_strategy='steps'			)
										trainer.train()
										snake_case:    Dict      							=trainer.callback_handler.callbacks[-2].events
										self.assertEqual(a_      ,						self.get_expected_events(a_			)			)

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

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









'''simple docstring'''

import unittest

from knapsack import knapsack as k






class 						a_		(     unittest.TestCase    ):


							def    UpperCamelCase     (				self   :						str			)			->					List[Any]:
										snake_case:    Optional[Any]      							=0
										snake_case:    List[str]      							=[0]
										snake_case:    List[str]      							=[0]
										snake_case:    Union[str, Any]      							=len(a_			)
										self.assertEqual(k.knapsack(a_      ,						a_      ,						a_      ,						a_			)      ,						0			)

										snake_case:    Union[str, Any]      							=[6_0]
										snake_case:    str      							=[1_0]
										snake_case:    List[str]      							=len(a_			)
										self.assertEqual(k.knapsack(a_      ,						a_      ,						a_      ,						a_			)      ,						0			)


							def    UpperCamelCase     (				self   :						int			)			->					Optional[Any]:
										snake_case:    Optional[int]      							=3
										snake_case:    Dict      							=[1, 2, 3]
										snake_case:    List[Any]      							=[3, 2, 1]
										snake_case:    Tuple      							=len(a_			)
										self.assertEqual(k.knapsack(a_      ,						a_      ,						a_      ,						a_			)      ,						5			)




							def    UpperCamelCase     (				self   :						List[str]			)			->					str:
										snake_case:    Any      							=5_0
										snake_case:    str      							=[6_0, 1_0_0, 1_2_0]
										snake_case:    Any      							=[1_0, 2_0, 3_0]
										snake_case:    Tuple      							=len(a_			)
										self.assertEqual(k.knapsack(a_      ,						a_      ,						a_      ,						a_			)      ,						2_2_0			)


if __name__ == "__main__":
	unittest.main()




