






"""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        lowercase__      (							snake_case__       ):


			def __init__(				self					:  Dict ):
										lowerCamelCase_	:							Optional[int]									=[]


			def      UpperCAmelCase__						(				self					:  Tuple       ,	snake_case__					:  Optional[int]       ,	snake_case__					:  str       ,	snake_case__					:  Optional[Any]       ,	**snake_case__					:  List[str] ):
										self.events.append("on_init_end" )


			def      UpperCAmelCase__						(				self					:  Optional[Any]       ,	snake_case__					:  str       ,	snake_case__					:  Optional[Any]       ,	snake_case__					:  Dict       ,	**snake_case__					:  Optional[Any] ):
										self.events.append("on_train_begin" )


			def      UpperCAmelCase__						(				self					:  Tuple       ,	snake_case__					:  str       ,	snake_case__					:  List[Any]       ,	snake_case__					:  Union[str, Any]       ,	**snake_case__					:  int ):
										self.events.append("on_train_end" )


			def      UpperCAmelCase__						(				self					:  int       ,	snake_case__					:  int       ,	snake_case__					:  Union[str, Any]       ,	snake_case__					:  Union[str, Any]       ,	**snake_case__					:  Optional[Any] ):
										self.events.append("on_epoch_begin" )


			def      UpperCAmelCase__						(				self					:  Any       ,	snake_case__					:  Any       ,	snake_case__					:  Dict       ,	snake_case__					:  Optional[Any]       ,	**snake_case__					:  Any ):
										self.events.append("on_epoch_end" )


			def      UpperCAmelCase__						(				self					:  List[str]       ,	snake_case__					:  Union[str, Any]       ,	snake_case__					:  Any       ,	snake_case__					:  Union[str, Any]       ,	**snake_case__					:  Tuple ):
										self.events.append("on_step_begin" )


			def      UpperCAmelCase__						(				self					:  Tuple       ,	snake_case__					:  List[str]       ,	snake_case__					:  Tuple       ,	snake_case__					:  List[str]       ,	**snake_case__					:  str ):
										self.events.append("on_step_end" )


			def      UpperCAmelCase__						(				self					:  Union[str, Any]       ,	snake_case__					:  Tuple       ,	snake_case__					:  Optional[int]       ,	snake_case__					:  int       ,	**snake_case__					:  Union[str, Any] ):
										self.events.append("on_evaluate" )


			def      UpperCAmelCase__						(				self					:  List[Any]       ,	snake_case__					:  Optional[Any]       ,	snake_case__					:  List[Any]       ,	snake_case__					:  str       ,	**snake_case__					:  Optional[Any] ):
										self.events.append("on_predict" )


			def      UpperCAmelCase__						(				self					:  List[Any]       ,	snake_case__					:  Tuple       ,	snake_case__					:  Optional[int]       ,	snake_case__					:  Dict       ,	**snake_case__					:  Any ):
										self.events.append("on_save" )


			def      UpperCAmelCase__						(				self					:  Dict       ,	snake_case__					:  Any       ,	snake_case__					:  str       ,	snake_case__					:  List[Any]       ,	**snake_case__					:  Union[str, Any] ):
										self.events.append("on_log" )


			def      UpperCAmelCase__						(				self					:  List[Any]       ,	snake_case__					:  List[str]       ,	snake_case__					:  Tuple       ,	snake_case__					:  Optional[Any]       ,	**snake_case__					:  str ):
										self.events.append("on_prediction_step" )







@require_torch
class        lowercase__      (							unittest.TestCase       ):


			def      UpperCAmelCase__						(				self					:  Dict ):
										lowerCamelCase_	:							Optional[int]									=tempfile.mkdtemp()


			def      UpperCAmelCase__						(				self					:  List[str] ):
										shutil.rmtree(self.output_dir )


			def      UpperCAmelCase__						(				self					:  List[Any]       ,	snake_case__					:  str=0       ,	snake_case__					:  Any=0       ,	snake_case__					:  str=64       ,	snake_case__					:  Union[str, Any]=64       ,	snake_case__					:  Any=None       ,	snake_case__					:  Tuple=False       ,	**snake_case__					:  str ):
										# 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.
										lowerCamelCase_	:							Tuple									=RegressionDataset(length=snake_case__ )
										lowerCamelCase_	:							Any									=RegressionDataset(length=snake_case__ )
										lowerCamelCase_	:							Tuple									=RegressionModelConfig(a=snake_case__       ,	b=snake_case__ )
										lowerCamelCase_	:							Optional[Any]									=RegressionPreTrainedModel(snake_case__ )

										lowerCamelCase_	:							Any									=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      UpperCAmelCase__						(				self					:  List[str]       ,	snake_case__					:  Optional[int]       ,	snake_case__					:  Tuple ):
										self.assertEqual(len(snake_case__ )       ,	len(snake_case__ ) )

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

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


			def      UpperCAmelCase__						(				self					:  List[Any] ):
										lowerCamelCase_	:							List[str]									=DEFAULT_CALLBACKS.copy() + [ProgressCallback]
										lowerCamelCase_	:							Optional[Any]									=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__ )

										lowerCamelCase_	:							List[str]									=self.get_trainer()
										lowerCamelCase_	:							Union[str, Any]									=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
										lowerCamelCase_	:							Union[str, Any]									=self.get_trainer()
										lowerCamelCase_	:							Union[str, Any]									=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__ )

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

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

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

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

										lowerCamelCase_	:							Tuple									=self.get_trainer(callbacks=[MyTestTrainerCallback]       ,	eval_steps=5       ,	evaluation_strategy="steps" )
										trainer.train()
										lowerCamelCase_	:							str									=trainer.callback_handler.callbacks[-2].events
										self.assertEqual(snake_case__       ,	self.get_expected_events(snake_case__ ) )

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

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











"""simple docstring"""



import pytest

from datasets.parallel import ParallelBackendConfig, parallel_backend
from datasets.utils.py_utils import map_nested

from .utils import require_dill_gt_0_3_2, require_joblibspark, require_not_windows

def 			_snake_case    (					lowerCamelCase__     :   Any  )    ->     Union[str, Any]:  # picklable for multiprocessing
							return i + 1

@require_dill_gt_0_3_2
@require_joblibspark
@require_not_windows
def 			_snake_case    (					)    ->     List[Any]:
							with parallel_backend("spark"  ):
														assert ParallelBackendConfig.backend_name == "spark"

							lowerCamelCase_	:							Optional[Any]									=[1, 2, 3]
							with pytest.raises(lowerCamelCase__  ):
														with parallel_backend("unsupported backend"  ):
																					map_nested(lowerCamelCase__    ,						lowerCamelCase__    ,						num_proc=2  )

							with pytest.raises(lowerCamelCase__  ):
														with parallel_backend("unsupported backend"  ):
																					map_nested(lowerCamelCase__    ,						lowerCamelCase__    ,						num_proc=-1  )

@require_dill_gt_0_3_2
@require_joblibspark
@require_not_windows
@pytest.mark.parametrize("num_proc"    ,						[2, -1]  )
def 			_snake_case    (					lowerCamelCase__     :   Tuple  )    ->     Optional[Any]:
							lowerCamelCase_	:							str									=[1, 2]
							lowerCamelCase_	:							List[str]									={"a": 1, "b": 2}
							lowerCamelCase_	:							List[str]									={"a": [1, 2], "b": [3, 4]}
							lowerCamelCase_	:							Optional[int]									={"a": {"1": 1}, "b": 2}
							lowerCamelCase_	:							int									={"a": 1, "b": 2, "c": 3, "d": 4}
							lowerCamelCase_	:							Optional[int]									=[2, 3]
							lowerCamelCase_	:							List[Any]									={"a": 2, "b": 3}
							lowerCamelCase_	:							int									={"a": [2, 3], "b": [4, 5]}
							lowerCamelCase_	:							str									={"a": {"1": 2}, "b": 3}
							lowerCamelCase_	:							Dict									={"a": 2, "b": 3, "c": 4, "d": 5}

							with parallel_backend("spark"  ):
														assert map_nested(lowerCamelCase__    ,						lowerCamelCase__    ,						num_proc=lowerCamelCase__  ) == expected_map_nested_sa
														assert map_nested(lowerCamelCase__    ,						lowerCamelCase__    ,						num_proc=lowerCamelCase__  ) == expected_map_nested_sa
														assert map_nested(lowerCamelCase__    ,						lowerCamelCase__    ,						num_proc=lowerCamelCase__  ) == expected_map_nested_sa
														assert map_nested(lowerCamelCase__    ,						lowerCamelCase__    ,						num_proc=lowerCamelCase__  ) == expected_map_nested_sa
														assert map_nested(lowerCamelCase__    ,						lowerCamelCase__    ,						num_proc=lowerCamelCase__  ) == expected_map_nested_sa



