
import inspect
import unittest

from transformers import YolosConfig
from transformers.testing_utils import require_torch, require_vision, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
	import torch
	from torch import nn

	from transformers import YolosForObjectDetection, YolosModel
	from transformers.models.yolos.modeling_yolos import YOLOS_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
	from PIL import Image

	from transformers import AutoImageProcessor







class        A_   :




	'''simple docstring'''


	def __init__(       self		,    snake_case		,    snake_case=13		,    snake_case=[30, 30]		,    snake_case=2		,    snake_case=3		,    snake_case=True		,    snake_case=True		,    snake_case=32		,    snake_case=5		,    snake_case=4		,    snake_case=37		,    snake_case="gelu"		,    snake_case=0.1		,    snake_case=0.1		,    snake_case=10		,    snake_case=0.02		,    snake_case=3		,    snake_case=None		,    snake_case=8		,    snake_case=10		,    ):
		lowercase       =    parent
		lowercase       =    batch_size
		lowercase       =    image_size
		lowercase       =    patch_size
		lowercase       =    num_channels
		lowercase       =    is_training
		lowercase       =    use_labels
		lowercase       =    hidden_size
		lowercase       =    num_hidden_layers
		lowercase       =    num_attention_heads
		lowercase       =    intermediate_size
		lowercase       =    hidden_act
		lowercase       =    hidden_dropout_prob
		lowercase       =    attention_probs_dropout_prob
		lowercase       =    type_sequence_label_size
		lowercase       =    initializer_range
		lowercase       =    num_labels
		lowercase       =    scope
		lowercase       =    n_targets
		lowercase       =    num_detection_tokens
		# we set the expected sequence length (which is used in several tests)
		# expected sequence length = num_patches + 1 (we add 1 for the [CLS] token) + num_detection_tokens
		lowercase       =    (image_size[1] // patch_size) * (image_size[0] // patch_size)
		lowercase       =    num_patches + 1 + self.num_detection_tokens





	def 	SCREAMING_SNAKE_CASE__						(       self       ):
		lowercase       =    floats_tensor([self.batch_size, self.num_channels, self.image_size[0], self.image_size[1]]       )

		lowercase       =    None
		if self.use_labels:
			# labels is a list of Dict (each Dict being the labels for a given example in the batch)
			lowercase       =    []
			for i in range(self.batch_size       ):
				lowercase       =    {}
				lowercase       =    torch.randint(
				    high=self.num_labels		,    size=(self.n_targets,)		,    device=snake_case       )
				lowercase       =    torch.rand(self.n_targets		,    4		,    device=snake_case       )
				labels.append(snake_case       )

		lowercase       =    self.get_config()

		return config, pixel_values, labels





	def 	SCREAMING_SNAKE_CASE__						(       self       ):
		return YolosConfig(
		    image_size=self.image_size		,    patch_size=self.patch_size		,    num_channels=self.num_channels		,    hidden_size=self.hidden_size		,    num_hidden_layers=self.num_hidden_layers		,    num_attention_heads=self.num_attention_heads		,    intermediate_size=self.intermediate_size		,    hidden_act=self.hidden_act		,    hidden_dropout_prob=self.hidden_dropout_prob		,    attention_probs_dropout_prob=self.attention_probs_dropout_prob		,    is_decoder=snake_case		,    initializer_range=self.initializer_range		,    num_detection_tokens=self.num_detection_tokens		,    num_labels=self.num_labels		,    )





	def 	SCREAMING_SNAKE_CASE__						(       self		,    snake_case		,    snake_case		,    snake_case       ):
		lowercase       =    YolosModel(config=snake_case       )
		model.to(snake_case       )
		model.eval()
		lowercase       =    model(snake_case       )
		self.parent.assertEqual(
		    result.last_hidden_state.shape		,    (self.batch_size, self.expected_seq_len, self.hidden_size)       )





	def 	SCREAMING_SNAKE_CASE__						(       self		,    snake_case		,    snake_case		,    snake_case       ):
		lowercase       =    YolosForObjectDetection(snake_case       )
		model.to(snake_case       )
		model.eval()

		lowercase       =    model(pixel_values=snake_case       )
		lowercase       =    model(snake_case       )

		self.parent.assertEqual(result.logits.shape		,    (self.batch_size, self.num_detection_tokens, self.num_labels + 1)       )
		self.parent.assertEqual(result.pred_boxes.shape		,    (self.batch_size, self.num_detection_tokens, 4)       )

		lowercase       =    model(pixel_values=snake_case		,    labels=snake_case       )

		self.parent.assertEqual(result.loss.shape		,    ()       )
		self.parent.assertEqual(result.logits.shape		,    (self.batch_size, self.num_detection_tokens, self.num_labels + 1)       )
		self.parent.assertEqual(result.pred_boxes.shape		,    (self.batch_size, self.num_detection_tokens, 4)       )





	def 	SCREAMING_SNAKE_CASE__						(       self       ):
		lowercase       =    self.prepare_config_and_inputs()
		lowercase  ,					lowercase  ,					lowercase       =    config_and_inputs
		lowercase       =    {'pixel_values': pixel_values}
		return config, inputs_dict







@require_torch
class        A_   (			__lowerCamelCase      ,	__lowerCamelCase      ,	unittest.TestCase   ):




	'''simple docstring'''


	_UpperCamelCase     :     int							 =       (YolosModel, YolosForObjectDetection) if is_torch_available() else ()
	_UpperCamelCase     :     Union[str, Any]							 =       (
	    {"""feature-extraction""": YolosModel, """object-detection""": YolosForObjectDetection} if is_torch_available() else {}
	)

	_UpperCamelCase     :     List[Any]							 =       False
	_UpperCamelCase     :     Optional[int]							 =       False
	_UpperCamelCase     :     Tuple							 =       False
	_UpperCamelCase     :     List[str]							 =       False





	def 	SCREAMING_SNAKE_CASE__						(       self		,    snake_case		,    snake_case		,    snake_case=False       ):
		lowercase       =    super()._prepare_for_class(snake_case		,    snake_case		,    return_labels=snake_case       )

		if return_labels:
			if model_class.__name__ == "YolosForObjectDetection":
				lowercase       =    []
				for i in range(self.model_tester.batch_size       ):
					lowercase       =    {}
					lowercase       =    torch.ones(
					    size=(self.model_tester.n_targets,)		,    device=snake_case		,    dtype=torch.long       )
					lowercase       =    torch.ones(
					    self.model_tester.n_targets		,    4		,    device=snake_case		,    dtype=torch.float       )
					labels.append(snake_case       )
				lowercase       =    labels

		return inputs_dict





	def 	SCREAMING_SNAKE_CASE__						(       self       ):
		lowercase       =    YolosModelTester(self       )
		lowercase       =    ConfigTester(self		,    config_class=snake_case		,    has_text_modality=snake_case		,    hidden_size=37       )





	def 	SCREAMING_SNAKE_CASE__						(       self       ):
		self.config_tester.run_common_tests()





	def 	SCREAMING_SNAKE_CASE__						(       self       ):
		# YOLOS does not use inputs_embeds
		pass





	def 	SCREAMING_SNAKE_CASE__						(       self       ):
		lowercase  ,					lowercase       =    self.model_tester.prepare_config_and_inputs_for_common()

		for model_class in self.all_model_classes:
			lowercase       =    model_class(snake_case       )
			self.assertIsInstance(model.get_input_embeddings()		,    (nn.Module)       )
			lowercase       =    model.get_output_embeddings()
			self.assertTrue(x is None or isinstance(snake_case		,    nn.Linear       )       )





	def 	SCREAMING_SNAKE_CASE__						(       self       ):
		lowercase  ,					lowercase       =    self.model_tester.prepare_config_and_inputs_for_common()

		for model_class in self.all_model_classes:
			lowercase       =    model_class(snake_case       )
			lowercase       =    inspect.signature(model.forward       )
			# signature.parameters is an OrderedDict => so arg_names order is deterministic
			lowercase       =    [*signature.parameters.keys()]

			lowercase       =    ['pixel_values']
			self.assertListEqual(arg_names[:1]		,    snake_case       )





	def 	SCREAMING_SNAKE_CASE__						(       self       ):
		lowercase       =    self.model_tester.prepare_config_and_inputs()
		self.model_tester.create_and_check_model(*snake_case       )





	def 	SCREAMING_SNAKE_CASE__						(       self       ):
		lowercase  ,					lowercase       =    self.model_tester.prepare_config_and_inputs_for_common()
		lowercase       =    True

		# in YOLOS, the seq_len is different
		lowercase       =    self.model_tester.expected_seq_len
		for model_class in self.all_model_classes:
			lowercase       =    True
			lowercase       =    False
			lowercase       =    True
			lowercase       =    model_class(snake_case       )
			model.to(snake_case       )
			model.eval()
			with torch.no_grad():
				lowercase       =    model(**self._prepare_for_class(snake_case		,    snake_case       )       )
			lowercase       =    outputs.attentions
			self.assertEqual(len(snake_case       )		,    self.model_tester.num_hidden_layers       )

			# check that output_attentions also work using config
			del inputs_dict["output_attentions"]
			lowercase       =    True
			lowercase       =    model_class(snake_case       )
			model.to(snake_case       )
			model.eval()
			with torch.no_grad():
				lowercase       =    model(**self._prepare_for_class(snake_case		,    snake_case       )       )
			lowercase       =    outputs.attentions
			self.assertEqual(len(snake_case       )		,    self.model_tester.num_hidden_layers       )

			self.assertListEqual(
			    list(attentions[0].shape[-3:]       )		,    [self.model_tester.num_attention_heads, seq_len, seq_len]		,    )
			lowercase       =    len(snake_case       )

			# Check attention is always last and order is fine
			lowercase       =    True
			lowercase       =    True
			lowercase       =    model_class(snake_case       )
			model.to(snake_case       )
			model.eval()
			with torch.no_grad():
				lowercase       =    model(**self._prepare_for_class(snake_case		,    snake_case       )       )

			lowercase       =    1
			self.assertEqual(out_len + added_hidden_states		,    len(snake_case       )       )

			lowercase       =    outputs.attentions

			self.assertEqual(len(snake_case       )		,    self.model_tester.num_hidden_layers       )
			self.assertListEqual(
			    list(self_attentions[0].shape[-3:]       )		,    [self.model_tester.num_attention_heads, seq_len, seq_len]		,    )





	def 	SCREAMING_SNAKE_CASE__						(       self       ):
		def check_hidden_states_output(snake_case		,    snake_case		,    snake_case       ):
			lowercase       =    model_class(snake_case       )
			model.to(snake_case       )
			model.eval()

			with torch.no_grad():
				lowercase       =    model(**self._prepare_for_class(snake_case		,    snake_case       )       )

			lowercase       =    outputs.hidden_states

			lowercase       =    getattr(
			    self.model_tester		,    'expected_num_hidden_layers'		,    self.model_tester.num_hidden_layers + 1       )
			self.assertEqual(len(snake_case       )		,    snake_case       )

			# YOLOS has a different seq_length
			lowercase       =    self.model_tester.expected_seq_len

			self.assertListEqual(
			    list(hidden_states[0].shape[-2:]       )		,    [seq_length, self.model_tester.hidden_size]		,    )

		lowercase  ,					lowercase       =    self.model_tester.prepare_config_and_inputs_for_common()

		for model_class in self.all_model_classes:
			lowercase       =    True
			check_hidden_states_output(snake_case		,    snake_case		,    snake_case       )

			# check that output_hidden_states also work using config
			del inputs_dict["output_hidden_states"]
			lowercase       =    True

			check_hidden_states_output(snake_case		,    snake_case		,    snake_case       )





	def 	SCREAMING_SNAKE_CASE__						(       self       ):
		lowercase       =    self.model_tester.prepare_config_and_inputs()
		self.model_tester.create_and_check_for_object_detection(*snake_case       )





	@slow
	def 	SCREAMING_SNAKE_CASE__						(       self       ):
		for model_name in YOLOS_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
			lowercase       =    YolosModel.from_pretrained(snake_case       )
			self.assertIsNotNone(snake_case       )






def 							UpperCAmelCase_		(							):
	lowercase       =    Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'							)
	return image







@require_torch
@require_vision
class        A_   (			unittest.TestCase   ):




	'''simple docstring'''


	@cached_property
	def 	SCREAMING_SNAKE_CASE__						(       self       ):
		return AutoImageProcessor.from_pretrained('hustvl/yolos-small'       ) if is_vision_available() else None





	@slow
	def 	SCREAMING_SNAKE_CASE__						(       self       ):
		lowercase       =    YolosForObjectDetection.from_pretrained('hustvl/yolos-small'       ).to(snake_case       )

		lowercase       =    self.default_image_processor
		lowercase       =    prepare_img()
		lowercase       =    image_processor(images=snake_case		,    return_tensors='pt'       ).to(snake_case       )

		# forward pass
		with torch.no_grad():
			lowercase       =    model(inputs.pixel_values       )

		# verify outputs
		lowercase       =    torch.Size((1, 100, 92)       )
		self.assertEqual(outputs.logits.shape		,    snake_case       )

		lowercase       =    torch.tensor(
		    [[-24.0_248, -10.3_024, -14.8_290], [-42.0_392, -16.8_200, -27.4_334], [-27.2_743, -11.8_154, -18.7_148]]		,    device=snake_case		,    )
		lowercase       =    torch.tensor(
		    [[0.2_559, 0.5_455, 0.4_706], [0.2_989, 0.7_279, 0.1_875], [0.7_732, 0.4_017, 0.4_462]]		,    device=snake_case       )
		self.assertTrue(torch.allclose(outputs.logits[0, :3, :3]		,    snake_case		,    atol=1E-4       )       )
		self.assertTrue(torch.allclose(outputs.pred_boxes[0, :3, :3]		,    snake_case		,    atol=1E-4       )       )

		# verify postprocessing
		lowercase       =    image_processor.post_process_object_detection(
		    snake_case		,    threshold=0.3		,    target_sizes=[image.size[::-1]]       )[0]
		lowercase       =    torch.tensor([0.9_994, 0.9_790, 0.9_964, 0.9_972, 0.9_861]       ).to(snake_case       )
		lowercase       =    [75, 75, 17, 63, 17]
		lowercase       =    torch.tensor([335.0_609, 79.3_848, 375.4_216, 187.2_495]       ).to(snake_case       )

		self.assertEqual(len(results['scores']       )		,    5       )
		self.assertTrue(torch.allclose(results['scores']		,    snake_case		,    atol=1E-4       )       )
		self.assertSequenceEqual(results['labels'].tolist()		,    snake_case       )
		self.assertTrue(torch.allclose(results['boxes'][0, :]		,    snake_case       )       )



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




	'''simple docstring'''


	def __init__(       self       ):
		lowercase       =    []





	def 	SCREAMING_SNAKE_CASE__						(       self		,    snake_case		,    snake_case		,    snake_case		,    **snake_case       ):
		self.events.append('on_init_end'       )





	def 	SCREAMING_SNAKE_CASE__						(       self		,    snake_case		,    snake_case		,    snake_case		,    **snake_case       ):
		self.events.append('on_train_begin'       )





	def 	SCREAMING_SNAKE_CASE__						(       self		,    snake_case		,    snake_case		,    snake_case		,    **snake_case       ):
		self.events.append('on_train_end'       )





	def 	SCREAMING_SNAKE_CASE__						(       self		,    snake_case		,    snake_case		,    snake_case		,    **snake_case       ):
		self.events.append('on_epoch_begin'       )





	def 	SCREAMING_SNAKE_CASE__						(       self		,    snake_case		,    snake_case		,    snake_case		,    **snake_case       ):
		self.events.append('on_epoch_end'       )





	def 	SCREAMING_SNAKE_CASE__						(       self		,    snake_case		,    snake_case		,    snake_case		,    **snake_case       ):
		self.events.append('on_step_begin'       )





	def 	SCREAMING_SNAKE_CASE__						(       self		,    snake_case		,    snake_case		,    snake_case		,    **snake_case       ):
		self.events.append('on_step_end'       )





	def 	SCREAMING_SNAKE_CASE__						(       self		,    snake_case		,    snake_case		,    snake_case		,    **snake_case       ):
		self.events.append('on_evaluate'       )





	def 	SCREAMING_SNAKE_CASE__						(       self		,    snake_case		,    snake_case		,    snake_case		,    **snake_case       ):
		self.events.append('on_predict'       )





	def 	SCREAMING_SNAKE_CASE__						(       self		,    snake_case		,    snake_case		,    snake_case		,    **snake_case       ):
		self.events.append('on_save'       )





	def 	SCREAMING_SNAKE_CASE__						(       self		,    snake_case		,    snake_case		,    snake_case		,    **snake_case       ):
		self.events.append('on_log'       )





	def 	SCREAMING_SNAKE_CASE__						(       self		,    snake_case		,    snake_case		,    snake_case		,    **snake_case       ):
		self.events.append('on_prediction_step'       )







@require_torch
class        A_   (			unittest.TestCase   ):




	'''simple docstring'''


	def 	SCREAMING_SNAKE_CASE__						(       self       ):
		lowercase       =    tempfile.mkdtemp()





	def 	SCREAMING_SNAKE_CASE__						(       self       ):
		shutil.rmtree(self.output_dir       )





	def 	SCREAMING_SNAKE_CASE__						(       self		,    snake_case=0		,    snake_case=0		,    snake_case=64		,    snake_case=64		,    snake_case=None		,    snake_case=False		,    **snake_case       ):
		# 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       =    RegressionDataset(length=snake_case       )
		lowercase       =    RegressionDataset(length=snake_case       )
		lowercase       =    RegressionModelConfig(a=snake_case		,    b=snake_case       )
		lowercase       =    RegressionPreTrainedModel(snake_case       )

		lowercase       =    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 	SCREAMING_SNAKE_CASE__						(       self		,    snake_case		,    snake_case       ):
		self.assertEqual(len(snake_case       )		,    len(snake_case       )       )

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

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





	def 	SCREAMING_SNAKE_CASE__						(       self       ):
		lowercase       =    DEFAULT_CALLBACKS.copy() + [ProgressCallback]
		lowercase       =    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       )

		lowercase       =    self.get_trainer()
		lowercase       =    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
		lowercase       =    self.get_trainer()
		lowercase       =    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       )

		lowercase       =    self.get_trainer()
		lowercase       =    trainer.callback_handler.callbacks[0]
		lowercase       =    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 	SCREAMING_SNAKE_CASE__						(       self       ):
		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       )

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

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

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

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

		lowercase       =    self.get_trainer(callbacks=[MyTestTrainerCallback]		,    evaluation_strategy='epoch'       )
		trainer.train()
		lowercase       =    trainer.callback_handler.callbacks[-2].events
		self.assertEqual(snake_case		,    self.get_expected_events(snake_case       )       )

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

