



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



       def __init__(					self   )	->				List[Any]:
           lowercase__									=    []



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



       def 				_a   (					self  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,**UpperCAmelCase_   )	->				Union[str, Any]:
           self.events.append("on_train_begin"   )



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



       def 				_a   (					self  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,**UpperCAmelCase_   )	->				str:
           self.events.append("on_epoch_begin"   )



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



       def 				_a   (					self  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,**UpperCAmelCase_   )	->				Tuple:
           self.events.append("on_step_begin"   )



       def 				_a   (					self  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,**UpperCAmelCase_   )	->				Union[str, Any]:
           self.events.append("on_step_end"   )



       def 				_a   (					self  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,**UpperCAmelCase_   )	->				List[str]:
           self.events.append("on_evaluate"   )



       def 				_a   (					self  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,**UpperCAmelCase_   )	->				Any:
           self.events.append("on_predict"   )



       def 				_a   (					self  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,**UpperCAmelCase_   )	->				List[str]:
           self.events.append("on_save"   )



       def 				_a   (					self  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,**UpperCAmelCase_   )	->				Union[str, Any]:
           self.events.append("on_log"   )







       def 				_a   (					self  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,**UpperCAmelCase_   )	->				Tuple:
           self.events.append("on_prediction_step"   )





@require_torch
class 					snake_case			(unittest.TestCase ):



       def 				_a   (					self   )	->				int:
           lowercase__									=    tempfile.mkdtemp()



       def 				_a   (					self   )	->				Any:
           shutil.rmtree(self.output_dir   )



       def 				_a   (					self  ,UpperCAmelCase_=0  ,UpperCAmelCase_=0  ,UpperCAmelCase_=64  ,UpperCAmelCase_=64  ,UpperCAmelCase_=None  ,UpperCAmelCase_=False  ,**UpperCAmelCase_   )	->				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.
           lowercase__									=    RegressionDataset(length=UpperCAmelCase_   )
           lowercase__									=    RegressionDataset(length=UpperCAmelCase_   )
           lowercase__									=    RegressionModelConfig(a=UpperCAmelCase_  ,b=UpperCAmelCase_   )
           lowercase__									=    RegressionPreTrainedModel(UpperCAmelCase_   )

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



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

           # Order doesn't matter
           lowercase__									=    sorted(UpperCAmelCase_  ,key=lambda UpperCAmelCase_   : cb.__name__ if isinstance(UpperCAmelCase_  ,UpperCAmelCase_   ) else cb.__class__.__name__   )
           lowercase__									=    sorted(UpperCAmelCase_  ,key=lambda UpperCAmelCase_   : 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 				_a   (					self  ,UpperCAmelCase_   )	->				Any:
           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(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 				_a   (					self   )	->				Union[str, Any]:
           lowercase__									=    self.get_trainer()
           lowercase__									=    DEFAULT_CALLBACKS.copy() + [ProgressCallback]
           self.check_callbacks_equality(trainer.callback_handler.callbacks  ,UpperCAmelCase_   )

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



       def 				_a   (					self   )	->				List[str]:
           lowercase__									=    DEFAULT_CALLBACKS.copy() + [ProgressCallback]
           lowercase__									=    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_   )

           lowercase__									=    self.get_trainer()
           lowercase__									=    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
           lowercase__									=    self.get_trainer()
           lowercase__									=    trainer.callback_handler.callbacks[0]
           trainer.remove_callback(UpperCAmelCase_   )
           expected_callbacks.remove(UpperCAmelCase_   )
           self.check_callbacks_equality(trainer.callback_handler.callbacks  ,UpperCAmelCase_   )

           lowercase__									=    self.get_trainer()
           lowercase__									=    trainer.callback_handler.callbacks[0]
           lowercase__									=    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 				_a   (					self   )	->				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=UpperCAmelCase_   )

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

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

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

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

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

           # 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(UpperCAmelCase_  ,self.get_expected_events(UpperCAmelCase_   )   )

           # 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(UpperCAmelCase_   ) in warn_mock.call_args[0][0]









'''simple docstring'''





SCREAMING_SNAKE_CASE__          =  256
# Modulus to hash a string
SCREAMING_SNAKE_CASE__          =  100_0003




def 							lowerCamelCase	(					_snake_case	:    str  ,_snake_case	:    str							):





    '''simple docstring'''


    lowercase__									=    len(_snake_case							)
    lowercase__									=    len(_snake_case							)
    if p_len > t_len:
        return False

    lowercase__									=    0
    lowercase__									=    0
    lowercase__									=    1

    # Calculating the hash of pattern and substring of text
    for i in range(_snake_case							):
        lowercase__									=    (ord(pattern[i]							) + p_hash * alphabet_size) % modulus
        lowercase__									=    (ord(text[i]							) + text_hash * alphabet_size) % modulus
        if i == p_len - 1:
            continue
        lowercase__									=    (modulus_power * alphabet_size) % modulus

    for i in range(0  ,t_len - p_len + 1							):
        if text_hash == p_hash and text[i : i + p_len] == pattern:
            return True
        if i == t_len - p_len:
            continue
        # Calculate the https://en.wikipedia.org/wiki/Rolling_hash
        lowercase__									=    (
            (text_hash - ord(text[i]							) * modulus_power) * alphabet_size
            + ord(text[i + p_len]							)
        ) % modulus
    return False




def 							lowerCamelCase	(					):





    '''simple docstring'''


    lowercase__									=    "abc1abc12"
    lowercase__									=    "alskfjaldsabc1abc1abc12k23adsfabcabc"
    lowercase__									=    "alskfjaldsk23adsfabcabc"
    assert rabin_karp(_snake_case  ,_snake_case							) and not rabin_karp(_snake_case  ,_snake_case							)

    # Test 2)
    lowercase__									=    "ABABX"
    lowercase__									=    "ABABZABABYABABX"
    assert rabin_karp(_snake_case  ,_snake_case							)

    # Test 3)
    lowercase__									=    "AAAB"
    lowercase__									=    "ABAAAAAB"
    assert rabin_karp(_snake_case  ,_snake_case							)

    # Test 4)
    lowercase__									=    "abcdabcy"
    lowercase__									=    "abcxabcdabxabcdabcdabcy"
    assert rabin_karp(_snake_case  ,_snake_case							)

    # Test 5)
    lowercase__									=    "Lü"
    lowercase__									=    "Lüsai"
    assert rabin_karp(_snake_case  ,_snake_case							)
    lowercase__									=    "Lue"
    assert not rabin_karp(_snake_case  ,_snake_case							)
    print("Success."							)


if __name__ == "__main__":
 test_rabin_karp()




