




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




			def __init__(					self							)				->				Any:

										'''simple docstring'''




										snake_case_						=      []




			def    lowerCAmelCase__				(					self				,							a__				,							a__				,							a__				,							**a__							)				->				Tuple:

										'''simple docstring'''




										self.events.append("on_init_end"							)




			def    lowerCAmelCase__				(					self				,							a__				,							a__				,							a__				,							**a__							)				->				List[str]:

										'''simple docstring'''




										self.events.append("on_train_begin"							)




			def    lowerCAmelCase__				(					self				,							a__				,							a__				,							a__				,							**a__							)				->				List[str]:

										'''simple docstring'''




										self.events.append("on_train_end"							)




			def    lowerCAmelCase__				(					self				,							a__				,							a__				,							a__				,							**a__							)				->				int:

										'''simple docstring'''




										self.events.append("on_epoch_begin"							)




			def    lowerCAmelCase__				(					self				,							a__				,							a__				,							a__				,							**a__							)				->				List[str]:

										'''simple docstring'''




										self.events.append("on_epoch_end"							)




			def    lowerCAmelCase__				(					self				,							a__				,							a__				,							a__				,							**a__							)				->				List[Any]:

										'''simple docstring'''




										self.events.append("on_step_begin"							)




			def    lowerCAmelCase__				(					self				,							a__				,							a__				,							a__				,							**a__							)				->				Tuple:

										'''simple docstring'''




										self.events.append("on_step_end"							)




			def    lowerCAmelCase__				(					self				,							a__				,							a__				,							a__				,							**a__							)				->				Dict:

										'''simple docstring'''




										self.events.append("on_evaluate"							)




			def    lowerCAmelCase__				(					self				,							a__				,							a__				,							a__				,							**a__							)				->				Dict:

										'''simple docstring'''




										self.events.append("on_predict"							)




			def    lowerCAmelCase__				(					self				,							a__				,							a__				,							a__				,							**a__							)				->				Optional[int]:

										'''simple docstring'''




										self.events.append("on_save"							)




			def    lowerCAmelCase__				(					self				,							a__				,							a__				,							a__				,							**a__							)				->				str:

										'''simple docstring'''




										self.events.append("on_log"							)




			def    lowerCAmelCase__				(					self				,							a__				,							a__				,							a__				,							**a__							)				->				Union[str, Any]:

										'''simple docstring'''




										self.events.append("on_prediction_step"							)




@require_torch
class  _snake_case		(	unittest.TestCase							):




			def    lowerCAmelCase__				(					self							)				->				Optional[int]:

										'''simple docstring'''




										snake_case_						=      tempfile.mkdtemp()




			def    lowerCAmelCase__				(					self							)				->				str:

										'''simple docstring'''




										shutil.rmtree(self.output_dir							)




			def    lowerCAmelCase__				(					self				,							a__=0				,							a__=0				,							a__=64				,							a__=64				,							a__=None				,							a__=False				,							**a__							)				->				Optional[Any]:

										'''simple docstring'''




										snake_case_						=      RegressionDataset(length=UpperCamelCase__							)
										snake_case_						=      RegressionDataset(length=UpperCamelCase__							)
										snake_case_						=      RegressionModelConfig(a=UpperCamelCase__				,							b=UpperCamelCase__							)
										snake_case_						=      RegressionPreTrainedModel(UpperCamelCase__							)

										snake_case_						=      TrainingArguments(self.output_dir				,							disable_tqdm=UpperCamelCase__				,							report_to=[]				,							**UpperCamelCase__							)
										return Trainer(
										    UpperCamelCase__				,							UpperCamelCase__				,							train_dataset=UpperCamelCase__				,							eval_dataset=UpperCamelCase__				,							callbacks=UpperCamelCase__				,							)




			def    lowerCAmelCase__				(					self				,							a__				,							a__							)				->				Optional[int]:

										'''simple docstring'''




										self.assertEqual(len(UpperCamelCase__							)				,							len(UpperCamelCase__							)							)

										# Order doesn't matter
										snake_case_						=      sorted(UpperCamelCase__				,							key=lambda a__							: cb.__name__ if isinstance(UpperCamelCase__				,							UpperCamelCase__							) else cb.__class__.__name__							)
										snake_case_						=      sorted(UpperCamelCase__				,							key=lambda a__							: cb.__name__ if isinstance(UpperCamelCase__				,							UpperCamelCase__							) else cb.__class__.__name__							)

										for cba, cba in zip(UpperCamelCase__				,							UpperCamelCase__							):
																	if isinstance(UpperCamelCase__				,							UpperCamelCase__							) and isinstance(UpperCamelCase__				,							UpperCamelCase__							):
																								self.assertEqual(UpperCamelCase__				,							UpperCamelCase__							)
																	elif isinstance(UpperCamelCase__				,							UpperCamelCase__							) and not isinstance(UpperCamelCase__				,							UpperCamelCase__							):
																								self.assertEqual(UpperCamelCase__				,							cba.__class__							)
																	elif not isinstance(UpperCamelCase__				,							UpperCamelCase__							) and isinstance(UpperCamelCase__				,							UpperCamelCase__							):
																								self.assertEqual(cba.__class__				,							UpperCamelCase__							)
																	else:
																								self.assertEqual(UpperCamelCase__				,							UpperCamelCase__							)




			def    lowerCAmelCase__				(					self				,							a__							)				->				Optional[Any]:

										'''simple docstring'''




										snake_case_						=      ["on_init_end", "on_train_begin"]
										snake_case_						=      0
										snake_case_						=      len(trainer.get_eval_dataloader()							)
										snake_case_						=      ["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(UpperCamelCase__							):
																								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    lowerCAmelCase__				(					self							)				->				List[str]:

										'''simple docstring'''




										snake_case_						=      self.get_trainer()
										snake_case_						=      DEFAULT_CALLBACKS.copy() + [ProgressCallback]
										self.check_callbacks_equality(trainer.callback_handler.callbacks				,							UpperCamelCase__							)

										# Callbacks passed at init are added to the default callbacks
										snake_case_						=      self.get_trainer(callbacks=[MyTestTrainerCallback]							)
										expected_callbacks.append(UpperCamelCase__							)
										self.check_callbacks_equality(trainer.callback_handler.callbacks				,							UpperCamelCase__							)

										# TrainingArguments.disable_tqdm controls if use ProgressCallback or PrinterCallback
										snake_case_						=      self.get_trainer(disable_tqdm=UpperCamelCase__							)
										snake_case_						=      DEFAULT_CALLBACKS.copy() + [PrinterCallback]
										self.check_callbacks_equality(trainer.callback_handler.callbacks				,							UpperCamelCase__							)




			def    lowerCAmelCase__				(					self							)				->				List[str]:

										'''simple docstring'''




										snake_case_						=      DEFAULT_CALLBACKS.copy() + [ProgressCallback]
										snake_case_						=      self.get_trainer()

										# We can add, pop, or remove by class name
										trainer.remove_callback(UpperCamelCase__							)
										expected_callbacks.remove(UpperCamelCase__							)
										self.check_callbacks_equality(trainer.callback_handler.callbacks				,							UpperCamelCase__							)

										snake_case_						=      self.get_trainer()
										snake_case_						=      trainer.pop_callback(UpperCamelCase__							)
										self.assertEqual(cb.__class__				,							UpperCamelCase__							)
										self.check_callbacks_equality(trainer.callback_handler.callbacks				,							UpperCamelCase__							)

										trainer.add_callback(UpperCamelCase__							)
										expected_callbacks.insert(0				,							UpperCamelCase__							)
										self.check_callbacks_equality(trainer.callback_handler.callbacks				,							UpperCamelCase__							)

										# We can also add, pop, or remove by instance
										snake_case_						=      self.get_trainer()
										snake_case_						=      trainer.callback_handler.callbacks[0]
										trainer.remove_callback(UpperCamelCase__							)
										expected_callbacks.remove(UpperCamelCase__							)
										self.check_callbacks_equality(trainer.callback_handler.callbacks				,							UpperCamelCase__							)

										snake_case_						=      self.get_trainer()
										snake_case_						=      trainer.callback_handler.callbacks[0]
										snake_case_						=      trainer.pop_callback(UpperCamelCase__							)
										self.assertEqual(UpperCamelCase__				,							UpperCamelCase__							)
										self.check_callbacks_equality(trainer.callback_handler.callbacks				,							UpperCamelCase__							)

										trainer.add_callback(UpperCamelCase__							)
										expected_callbacks.insert(0				,							UpperCamelCase__							)
										self.check_callbacks_equality(trainer.callback_handler.callbacks				,							UpperCamelCase__							)




			def    lowerCAmelCase__				(					self							)				->				List[str]:

										'''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=UpperCamelCase__							)

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

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

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

										snake_case_						=      self.get_trainer(callbacks=[MyTestTrainerCallback]				,							eval_steps=5				,							evaluation_strategy="steps"							)
										trainer.train()
										snake_case_						=      trainer.callback_handler.callbacks[-2].events
										self.assertEqual(UpperCamelCase__				,							self.get_expected_events(UpperCamelCase__							)							)

										snake_case_						=      self.get_trainer(callbacks=[MyTestTrainerCallback]				,							evaluation_strategy="epoch"							)
										trainer.train()
										snake_case_						=      trainer.callback_handler.callbacks[-2].events
										self.assertEqual(UpperCamelCase__				,							self.get_expected_events(UpperCamelCase__							)							)

										# A bit of everything
										snake_case_						=      self.get_trainer(
										    callbacks=[MyTestTrainerCallback]				,							logging_steps=3				,							save_steps=10				,							eval_steps=5				,							evaluation_strategy="steps"				,							)
										trainer.train()
										snake_case_						=      trainer.callback_handler.callbacks[-2].events
										self.assertEqual(UpperCamelCase__				,							self.get_expected_events(UpperCamelCase__							)							)

										# warning should be emitted for duplicated callbacks
										with patch("transformers.trainer_callback.logger.warning"							) as warn_mock:
																	snake_case_						=      self.get_trainer(
																	    callbacks=[MyTestTrainerCallback, MyTestTrainerCallback]				,							)
																	assert str(UpperCamelCase__							) in warn_mock.call_args[0][0]










def      A      (      _SCREAMING_SNAKE_CASE ,_SCREAMING_SNAKE_CASE    )	-> list:
       lowerCamelCase   :      Dict			 =					len(_SCREAMING_SNAKE_CASE    )
       lowerCamelCase   :      Union[str, Any]			 =					[]
       for i in range(len(_SCREAMING_SNAKE_CASE    ) - pat_len + 1    ):
              lowerCamelCase   :      Dict			 =					True
              for j in range(_SCREAMING_SNAKE_CASE    ):
                     if s[i + j] != pattern[j]:
                            lowerCamelCase   :      Optional[int]			 =					False
                            break
              if match_found:
                     position.append(_SCREAMING_SNAKE_CASE    )
       return position


if __name__ == "__main__":
       assert naive_pattern_search('ABCDEFG', 'DE') == [3]
       print(naive_pattern_search('ABAAABCDBBABCDDEBCABC', 'ABC'))






