

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 				UpperCAmelCase_		(			__lowercase   ):
							"""simple docstring"""







							def __init__(			self       )    ->    Union[str, Any]:
												_a	:    Optional[Any]         =		[]


							def 				__lowercase				(			self       ,							_a       ,							_a       ,							_a       ,							**_a       )    ->    Optional[Any]:
												self.events.append('''on_init_end'''       )


							def 				__lowercase				(			self       ,							_a       ,							_a       ,							_a       ,							**_a       )    ->    List[str]:
												self.events.append('''on_train_begin'''       )


							def 				__lowercase				(			self       ,							_a       ,							_a       ,							_a       ,							**_a       )    ->    Union[str, Any]:
												self.events.append('''on_train_end'''       )


							def 				__lowercase				(			self       ,							_a       ,							_a       ,							_a       ,							**_a       )    ->    Tuple:
												self.events.append('''on_epoch_begin'''       )


							def 				__lowercase				(			self       ,							_a       ,							_a       ,							_a       ,							**_a       )    ->    Optional[int]:
												self.events.append('''on_epoch_end'''       )


							def 				__lowercase				(			self       ,							_a       ,							_a       ,							_a       ,							**_a       )    ->    Optional[int]:
												self.events.append('''on_step_begin'''       )


							def 				__lowercase				(			self       ,							_a       ,							_a       ,							_a       ,							**_a       )    ->    int:
												self.events.append('''on_step_end'''       )


							def 				__lowercase				(			self       ,							_a       ,							_a       ,							_a       ,							**_a       )    ->    Any:
												self.events.append('''on_evaluate'''       )


							def 				__lowercase				(			self       ,							_a       ,							_a       ,							_a       ,							**_a       )    ->    List[str]:
												self.events.append('''on_predict'''       )


							def 				__lowercase				(			self       ,							_a       ,							_a       ,							_a       ,							**_a       )    ->    Union[str, Any]:
												self.events.append('''on_save'''       )


							def 				__lowercase				(			self       ,							_a       ,							_a       ,							_a       ,							**_a       )    ->    List[Any]:
												self.events.append('''on_log'''       )




							def 				__lowercase				(			self       ,							_a       ,							_a       ,							_a       ,							**_a       )    ->    int:
												self.events.append('''on_prediction_step'''       )



@require_torch
class 				UpperCAmelCase_		(			unittest.TestCase   ):
							"""simple docstring"""







							def 				__lowercase				(			self       )    ->    str:
												_a	:    List[Any]         =		tempfile.mkdtemp()


							def 				__lowercase				(			self       )    ->    Optional[Any]:
												shutil.rmtree(self.output_dir       )


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

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


							def 				__lowercase				(			self       ,							_a       ,							_a       )    ->    Optional[Any]:
												self.assertEqual(len(_a       )       ,							len(_a       )       )

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

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


							def 				__lowercase				(			self       )    ->    int:
												_a	:    str         =		DEFAULT_CALLBACKS.copy() + [ProgressCallback]
												_a	:    Dict         =		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       )

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

												_a	:    Dict         =		self.get_trainer()
												_a	:    str         =		trainer.callback_handler.callbacks[0]
												_a	:    Tuple         =		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 				__lowercase				(			self       )    ->    Optional[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       )

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

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

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

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

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

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






import re

import jax.numpy as jnp
from flax.traverse_util import flatten_dict, unflatten_dict
from jax.random import PRNGKey

from ..utils import logging


a__				=		logging.get_logger(__name__)



def    __UpperCAmelCase (	__a      :			Dict	)			->     Tuple:

					"""simple docstring"""

					_a	:    Any         =		R'''\w+[.]\d+'''
					_a	:    Union[str, Any]         =		re.findall(__a		,__a	)
					for pat in pats:
										_a	:    int         =		key.replace(__a		,'''_'''.join(pat.split('''.'''	)	)	)
					return key



def    __UpperCAmelCase (	__a      :			List[str]		,__a      :			Union[str, Any]		,__a      :			Optional[int]	)			->     Tuple:

					"""simple docstring"""

					_a	:    Dict         =		pt_tuple_key[:-1] + ('''scale''',)
					if (
					    any('''norm''' in str_ for str_ in pt_tuple_key	)
					    and (pt_tuple_key[-1] == "bias")
					    and (pt_tuple_key[:-1] + ("bias",) not in random_flax_state_dict)
					    and (pt_tuple_key[:-1] + ("scale",) in random_flax_state_dict)
					):
										_a	:    Dict         =		pt_tuple_key[:-1] + ('''scale''',)
										return renamed_pt_tuple_key, pt_tensor
					elif pt_tuple_key[-1] in ["weight", "gamma"] and pt_tuple_key[:-1] + ("scale",) in random_flax_state_dict:
										_a	:    Optional[int]         =		pt_tuple_key[:-1] + ('''scale''',)
										return renamed_pt_tuple_key, pt_tensor

					# embedding
					if pt_tuple_key[-1] == "weight" and pt_tuple_key[:-1] + ("embedding",) in random_flax_state_dict:
										_a	:    Union[str, Any]         =		pt_tuple_key[:-1] + ('''embedding''',)
										return renamed_pt_tuple_key, pt_tensor

					# conv layer
					_a	:    Tuple         =		pt_tuple_key[:-1] + ('''kernel''',)
					if pt_tuple_key[-1] == "weight" and pt_tensor.ndim == 4:
										_a	:    List[str]         =		pt_tensor.transpose(2		,3		,1		,0	)
										return renamed_pt_tuple_key, pt_tensor

					# linear layer
					_a	:    Dict         =		pt_tuple_key[:-1] + ('''kernel''',)
					if pt_tuple_key[-1] == "weight":
										_a	:    Union[str, Any]         =		pt_tensor.T
										return renamed_pt_tuple_key, pt_tensor

					# old PyTorch layer norm weight
					_a	:    Dict         =		pt_tuple_key[:-1] + ('''weight''',)
					if pt_tuple_key[-1] == "gamma":
										return renamed_pt_tuple_key, pt_tensor

					# old PyTorch layer norm bias
					_a	:    Union[str, Any]         =		pt_tuple_key[:-1] + ('''bias''',)
					if pt_tuple_key[-1] == "beta":
										return renamed_pt_tuple_key, pt_tensor

					return pt_tuple_key, pt_tensor







def    __UpperCAmelCase (	__a      :			Dict		,__a      :			str		,__a      :			str=42	)			->     Optional[int]:

					"""simple docstring"""

					_a	:    Tuple         =		{k: v.numpy() for k, v in pt_state_dict.items()}

					# Step 2: Since the model is stateless, get random Flax params
					_a	:    List[Any]         =		flax_model.init_weights(PRNGKey(__a	)	)

					_a	:    Optional[int]         =		flatten_dict(__a	)
					_a	:    Tuple         =		{}

					# Need to change some parameters name to match Flax names
					for pt_key, pt_tensor in pt_state_dict.items():
										_a	:    List[str]         =		rename_key(__a	)
										_a	:    Optional[Any]         =		tuple(renamed_pt_key.split('''.'''	)	)

										# Correctly rename weight parameters
										_a      ,					_a	:    List[str]         =		rename_key_and_reshape_tensor(__a		,__a		,__a	)

										if flax_key in random_flax_state_dict:
															if flax_tensor.shape != random_flax_state_dict[flax_key].shape:
																				raise ValueError(
																				    F"""PyTorch checkpoint seems to be incorrect. Weight {pt_key} was expected to be of shape """
																				    F"""{random_flax_state_dict[flax_key].shape}, but is {flax_tensor.shape}."""	)

        # also add unexpected weight so that warning is thrown
										_a	:    Dict         =		jnp.asarray(__a	)

					return unflatten_dict(__a	)



