






import argparse
import json
import os

import tensorstore as ts
import torch
from flax import serialization
from flax.traverse_util import flatten_dict, unflatten_dict
from tensorflow.io import gfile

from transformers.modeling_utils import dtype_byte_size
from transformers.models.switch_transformers.convert_switch_transformers_original_flax_checkpoint_to_pytorch import (
    rename_keys,
)
from transformers.utils import WEIGHTS_INDEX_NAME, WEIGHTS_NAME
from transformers.utils.hub import convert_file_size_to_int






def 					lowerCamelCase__					(_UpperCAmelCase				,							_UpperCAmelCase):
			if flax_key_tuple[-1] == "kernel" and flax_tensor.ndim == 3:
						# expert layer
						SCREAMING_SNAKE_CASE							=			flax_key_tuple[:-1] + ('weight',)
						SCREAMING_SNAKE_CASE							=			torch.permute(_UpperCAmelCase				,							(0, 2, 1))
			elif flax_key_tuple[-1] == "kernel" and ".".join(_UpperCAmelCase):
						# linear layer
						SCREAMING_SNAKE_CASE							=			flax_key_tuple[:-1] + ('weight',)
						SCREAMING_SNAKE_CASE							=			flax_tensor.T
			elif flax_key_tuple[-1] in ["scale", "embedding"]:
						SCREAMING_SNAKE_CASE							=			flax_key_tuple[:-1] + ('weight',)

			return flax_key_tuple, flax_tensor






def 					lowerCamelCase__					(_UpperCAmelCase				,							_UpperCAmelCase				,							_UpperCAmelCase):
			if "metadata" in layer:
						SCREAMING_SNAKE_CASE							=			layer.split('metadata')
						SCREAMING_SNAKE_CASE							=			''.join(split_layer[0])[:-1]
						SCREAMING_SNAKE_CASE							=			[tuple(('metadata' + split_layer[1]).split('/'))]
			elif "kvstore" in layer:
						SCREAMING_SNAKE_CASE							=			layer.split('kvstore')
						SCREAMING_SNAKE_CASE							=			''.join(split_layer[0])[:-1]
						SCREAMING_SNAKE_CASE							=			[tuple(('kvstore' + split_layer[1]).split('/'))]

			else:
						SCREAMING_SNAKE_CASE							=			layer.split('/')
						SCREAMING_SNAKE_CASE							=			'/'.join(split_layer[:-1])
						SCREAMING_SNAKE_CASE							=			(split_layer[-1],)

			if "kvstore/path" in layer:
						SCREAMING_SNAKE_CASE							=			F'''{switch_checkpoint_path}/{checkpoint_info[layer]}'''
			elif "kvstore/driver" in layer:
						SCREAMING_SNAKE_CASE							=			'file'
			else:
						SCREAMING_SNAKE_CASE							=			checkpoint_info[layer]

			return curr_real_layer_name, split_layer, content






def 					lowerCamelCase__					(_UpperCAmelCase				,							_UpperCAmelCase):
			SCREAMING_SNAKE_CASE							=			rename_keys(_UpperCAmelCase)
			SCREAMING_SNAKE_CASE							=			{}
			for k, v in current_block.items():
						SCREAMING_SNAKE_CASE							=			v
			SCREAMING_SNAKE_CASE							=			new_current_block
			torch.save(_UpperCAmelCase				,							_UpperCAmelCase)






def 					lowerCamelCase__					(_UpperCAmelCase				,							_UpperCAmelCase				,							_UpperCAmelCase				,							_UpperCAmelCase				,							_UpperCAmelCase = WEIGHTS_NAME):
			SCREAMING_SNAKE_CASE							=			convert_file_size_to_int(_UpperCAmelCase)
			SCREAMING_SNAKE_CASE							=			[]
			SCREAMING_SNAKE_CASE							=			{}
			SCREAMING_SNAKE_CASE							=			0
			SCREAMING_SNAKE_CASE							=			0

			os.makedirs(_UpperCAmelCase				,							exist_ok=_UpperCAmelCase)
			with gfile.GFile(switch_checkpoint_path + '/checkpoint'				,							'rb') as fp:
						SCREAMING_SNAKE_CASE							=			serialization.msgpack_restore(fp.read())['optimizer']['target']
						SCREAMING_SNAKE_CASE							=			flatten_dict(_UpperCAmelCase				,							sep='/')

			SCREAMING_SNAKE_CASE							=			{}
			for layer in checkpoint_info.keys():
						SCREAMING_SNAKE_CASE					,    SCREAMING_SNAKE_CASE					,    SCREAMING_SNAKE_CASE							=			get_key_and_tensorstore_dict(
						    _UpperCAmelCase				,							_UpperCAmelCase				,							_UpperCAmelCase)
						if curr_real_layer_name in all_layers:
									SCREAMING_SNAKE_CASE							=			content
						else:
									SCREAMING_SNAKE_CASE							=			{split_layer[-1]: content}

			for key in all_layers.keys():
						# open tensorstore file
						SCREAMING_SNAKE_CASE							=			ts.open(unflatten_dict(all_layers[key])).result().read().result()
						SCREAMING_SNAKE_CASE							=			torch.tensor(_UpperCAmelCase)
						SCREAMING_SNAKE_CASE							=			raw_weights.numel() * dtype_byte_size(raw_weights.dtype)

						# use the renaming pattern from the small conversion scripts
						SCREAMING_SNAKE_CASE					,    SCREAMING_SNAKE_CASE							=			rename_base_flax_keys(tuple(key.split('/'))				,							_UpperCAmelCase)
						SCREAMING_SNAKE_CASE							=			'/'.join(_UpperCAmelCase)

						# If this weight is going to tip up over the maximal size, we split.
						if current_block_size + weight_size > max_shard_size:
									SCREAMING_SNAKE_CASE							=			os.path.join(
									    _UpperCAmelCase				,							weights_name.replace('.bin'				,							F'''-{len(_UpperCAmelCase)+1:05d}-of-???.bin'''))
									rename_and_save_block(_UpperCAmelCase				,							_UpperCAmelCase)
									sharded_state_dicts.append(current_block.keys())
									del current_block
									SCREAMING_SNAKE_CASE							=			{}
									SCREAMING_SNAKE_CASE							=			0

						SCREAMING_SNAKE_CASE							=			raw_weights.to(getattr(_UpperCAmelCase				,							_UpperCAmelCase))
						current_block_size += weight_size
						total_size += weight_size

			# Add the last block
			SCREAMING_SNAKE_CASE							=			os.path.join(_UpperCAmelCase				,							weights_name.replace('.bin'				,							F'''-{len(_UpperCAmelCase)+1:05d}-of-???.bin'''))
			rename_and_save_block(_UpperCAmelCase				,							_UpperCAmelCase)
			sharded_state_dicts.append(current_block.keys())

			# If we only have one shard, we return it
			if len(_UpperCAmelCase) == 1:
						return {weights_name: sharded_state_dicts[0]}, None

			# Otherwise, let's build the index
			SCREAMING_SNAKE_CASE							=			{}
			SCREAMING_SNAKE_CASE							=			{}
			for idx, shard in enumerate(_UpperCAmelCase):
						SCREAMING_SNAKE_CASE							=			weights_name.replace(
						    '.bin'				,							F'''-{idx+1:05d}-of-{len(_UpperCAmelCase):05d}.bin''')  # len(sharded_state_dicts):05d}
						SCREAMING_SNAKE_CASE							=			os.path.join(_UpperCAmelCase				,							weights_name.replace('.bin'				,							F'''-{idx+1:05d}-of-???.bin'''))
						os.rename(_UpperCAmelCase				,							os.path.join(_UpperCAmelCase				,							_UpperCAmelCase))
						SCREAMING_SNAKE_CASE							=			shard
						for key in shard:
									SCREAMING_SNAKE_CASE							=			shard_file

    # Add the metadata
			SCREAMING_SNAKE_CASE							=			{'total_size': total_size}
			SCREAMING_SNAKE_CASE							=			{'metadata': metadata, 'weight_map': weight_map}

			with open(os.path.join(_UpperCAmelCase				,							_UpperCAmelCase)				,							'w'				,							encoding='utf-8') as f:
						SCREAMING_SNAKE_CASE							=			json.dumps(_UpperCAmelCase				,							indent=2				,							sort_keys=_UpperCAmelCase) + '\n'
						f.write(_UpperCAmelCase)

			return metadata, index


if __name__ == "__main__":
				a_		:    Any   							=     argparse.ArgumentParser()
				# Required parameters
				parser.add_argument(
				    '--switch_t5x_checkpoint_path',
				    default='/mnt/disks/disk_switch/original_checkpoints/switch-xxl-128/checkpoint_634600',
				    type=str,
				    required=False,
				    help='Path to a directory containing a folder per layer. Follows the original Google format.',
				)
				parser.add_argument('--max_shard_size', default='10GB', required=False, help='Max shard size')
				parser.add_argument('--dtype', default='bfloat16', type=str, required=False, help='dtype of the saved model')
				parser.add_argument(
				    '--pytorch_dump_folder_path',
				    default='/mnt/disks/disk_switch/original_checkpoints/switch-xxl-128-converted',
				    type=str,
				    required=False,
				    help='Path to the output pytorch model.',
				)
				a_		:    Any   							=     parser.parse_args()
				shard_on_the_fly(
				    args.switch_tax_checkpoint_path,
				    args.pytorch_dump_folder_path,
				    args.max_shard_size,
				    args.dtype,
				)







def 					lowerCamelCase__					():
			from transformers import SwitchTransformersConfig, SwitchTransformersForConditionalGeneration, TaTokenizer

			SCREAMING_SNAKE_CASE							=			SwitchTransformersConfig.from_pretrained('google/switch-base-8')
			config.save_pretrained('/home/arthur_huggingface_co/transformers/switch_converted')
			SCREAMING_SNAKE_CASE							=			SwitchTransformersForConditionalGeneration.from_pretrained(
			    '/home/arthur_huggingface_co/transformers/switch_converted'				,							device_map='auto')

			SCREAMING_SNAKE_CASE							=			TaTokenizer.from_pretrained('t5-small')
			SCREAMING_SNAKE_CASE							=			'A <extra_id_0> walks into a bar a orders a <extra_id_1> with <extra_id_2> pinch of <extra_id_3>.'

			SCREAMING_SNAKE_CASE							=			tokenizer(_UpperCAmelCase				,							return_tensors='pt').input_ids
			SCREAMING_SNAKE_CASE							=			model.generate(_UpperCAmelCase				,							decoder_start_token_id=0)
			print(tokenizer.decode(out[0]))






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    _snake_case (						UpperCAmelCase_				):


 def __init__(       self):


   '''simple docstring'''




   lowercase__	: List[Any]     		=   []


 def   lowercase__  (       self       ,	SCREAMING_SNAKE_CASE_       ,	SCREAMING_SNAKE_CASE_       ,	SCREAMING_SNAKE_CASE_       ,	**SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




   self.events.append("""on_init_end""")


 def   lowercase__  (       self       ,	SCREAMING_SNAKE_CASE_       ,	SCREAMING_SNAKE_CASE_       ,	SCREAMING_SNAKE_CASE_       ,	**SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




   self.events.append("""on_train_begin""")


 def   lowercase__  (       self       ,	SCREAMING_SNAKE_CASE_       ,	SCREAMING_SNAKE_CASE_       ,	SCREAMING_SNAKE_CASE_       ,	**SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




   self.events.append("""on_train_end""")


 def   lowercase__  (       self       ,	SCREAMING_SNAKE_CASE_       ,	SCREAMING_SNAKE_CASE_       ,	SCREAMING_SNAKE_CASE_       ,	**SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




   self.events.append("""on_epoch_begin""")


 def   lowercase__  (       self       ,	SCREAMING_SNAKE_CASE_       ,	SCREAMING_SNAKE_CASE_       ,	SCREAMING_SNAKE_CASE_       ,	**SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




   self.events.append("""on_epoch_end""")


 def   lowercase__  (       self       ,	SCREAMING_SNAKE_CASE_       ,	SCREAMING_SNAKE_CASE_       ,	SCREAMING_SNAKE_CASE_       ,	**SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




   self.events.append("""on_step_begin""")


 def   lowercase__  (       self       ,	SCREAMING_SNAKE_CASE_       ,	SCREAMING_SNAKE_CASE_       ,	SCREAMING_SNAKE_CASE_       ,	**SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




   self.events.append("""on_step_end""")


 def   lowercase__  (       self       ,	SCREAMING_SNAKE_CASE_       ,	SCREAMING_SNAKE_CASE_       ,	SCREAMING_SNAKE_CASE_       ,	**SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




   self.events.append("""on_evaluate""")


 def   lowercase__  (       self       ,	SCREAMING_SNAKE_CASE_       ,	SCREAMING_SNAKE_CASE_       ,	SCREAMING_SNAKE_CASE_       ,	**SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




   self.events.append("""on_predict""")


 def   lowercase__  (       self       ,	SCREAMING_SNAKE_CASE_       ,	SCREAMING_SNAKE_CASE_       ,	SCREAMING_SNAKE_CASE_       ,	**SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




   self.events.append("""on_save""")


 def   lowercase__  (       self       ,	SCREAMING_SNAKE_CASE_       ,	SCREAMING_SNAKE_CASE_       ,	SCREAMING_SNAKE_CASE_       ,	**SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




   self.events.append("""on_log""")



 def   lowercase__  (       self       ,	SCREAMING_SNAKE_CASE_       ,	SCREAMING_SNAKE_CASE_       ,	SCREAMING_SNAKE_CASE_       ,	**SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




   self.events.append("""on_prediction_step""")



@require_torch
class    _snake_case (						unittest.TestCase				):


 def   lowercase__  (       self):


   '''simple docstring'''




   lowercase__	: Dict     		=   tempfile.mkdtemp()


 def   lowercase__  (       self):


   '''simple docstring'''




   shutil.rmtree(self.output_dir)


 def   lowercase__  (       self       ,	SCREAMING_SNAKE_CASE_=0       ,	SCREAMING_SNAKE_CASE_=0       ,	SCREAMING_SNAKE_CASE_=64       ,	SCREAMING_SNAKE_CASE_=64       ,	SCREAMING_SNAKE_CASE_=None       ,	SCREAMING_SNAKE_CASE_=False       ,	**SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




   lowercase__	: Any     		=   RegressionDataset(length=SCREAMING_SNAKE_CASE_)
   lowercase__	: Optional[int]     		=   RegressionDataset(length=SCREAMING_SNAKE_CASE_)
   lowercase__	: Dict     		=   RegressionModelConfig(a=SCREAMING_SNAKE_CASE_       ,	b=SCREAMING_SNAKE_CASE_)
   lowercase__	: Any     		=   RegressionPreTrainedModel(SCREAMING_SNAKE_CASE_)

   lowercase__	: Any     		=   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   lowercase__  (       self       ,	SCREAMING_SNAKE_CASE_       ,	SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




   self.assertEqual(len(SCREAMING_SNAKE_CASE_)       ,	len(SCREAMING_SNAKE_CASE_))

   # Order doesn't matter
   lowercase__	: str     		=   sorted(SCREAMING_SNAKE_CASE_       ,	key=lambda SCREAMING_SNAKE_CASE_: cb.__name__ if isinstance(SCREAMING_SNAKE_CASE_       ,	SCREAMING_SNAKE_CASE_) else cb.__class__.__name__)
   lowercase__	: Tuple     		=   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   lowercase__  (       self       ,	SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




   lowercase__	: int     		=   ["""on_init_end""", """on_train_begin"""]
   lowercase__	: Union[str, Any]     		=   0
   lowercase__	: Union[str, Any]     		=   len(trainer.get_eval_dataloader())
   lowercase__	: Dict     		=   ["""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   lowercase__  (       self):


   '''simple docstring'''




   lowercase__	: int     		=   self.get_trainer()
   lowercase__	: Union[str, Any]     		=   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
   lowercase__	: Any     		=   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
   lowercase__	: Any     		=   self.get_trainer(disable_tqdm=SCREAMING_SNAKE_CASE_)
   lowercase__	: Tuple     		=   DEFAULT_CALLBACKS.copy() + [PrinterCallback]
   self.check_callbacks_equality(trainer.callback_handler.callbacks       ,	SCREAMING_SNAKE_CASE_)


 def   lowercase__  (       self):


   '''simple docstring'''




   lowercase__	: Any     		=   DEFAULT_CALLBACKS.copy() + [ProgressCallback]
   lowercase__	: Tuple     		=   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_)

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

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


   '''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_)

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

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

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

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

   lowercase__	: Tuple     		=   self.get_trainer(callbacks=[MyTestTrainerCallback]       ,	evaluation_strategy="""epoch""")
   trainer.train()
   lowercase__	: Optional[int]     		=   trainer.callback_handler.callbacks[-2].events
   self.assertEqual(SCREAMING_SNAKE_CASE_       ,	self.get_expected_events(SCREAMING_SNAKE_CASE_))

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






