




'''simple docstring'''







import math
import time

from transformers import Trainer, is_torch_tpu_available
from transformers.trainer_utils import PredictionOutput, speed_metrics


if is_torch_tpu_available(check_device=False):
   import torch_xla.core.xla_model as xm
   import torch_xla.debug.metrics as met


class 			_a  (	__a ):
       def __init__(	self						:	Optional[int]   ,				*lowercase						:	Optional[Any]   ,				lowercase						:	Optional[Any]=None   ,				lowercase						:	Tuple=None   ,				**lowercase						:	Dict				):



           '''simple docstring'''

           super().__init__(*lowercase   ,				**lowercase				)
           UpperCAmelCase			       =		eval_examples
           UpperCAmelCase			       =		post_process_function
       def     A      (	self						:	str   ,				lowercase						:	List[Any]=None   ,				lowercase						:	Tuple=None   ,				lowercase						:	Optional[Any]=None   ,				lowercase						:	str = "eval"				):



           '''simple docstring'''

           UpperCAmelCase			       =		self.eval_dataset if eval_dataset is None else eval_dataset
           UpperCAmelCase			       =		self.get_eval_dataloader(lowercase				)
           UpperCAmelCase			       =		self.eval_examples if eval_examples is None else eval_examples

           # Temporarily disable metric computation, we will do it in the loop here.
           UpperCAmelCase			       =		self.compute_metrics
           UpperCAmelCase			       =		None
           UpperCAmelCase			       =		self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop
           UpperCAmelCase			       =		time.time()
           try:
               UpperCAmelCase			       =		eval_loop(
                   lowercase   ,				description='''Evaluation'''   ,				prediction_loss_only=True if compute_metrics is None else None   ,				ignore_keys=lowercase   ,				metric_key_prefix=lowercase   ,				)
           finally:
               UpperCAmelCase			       =		compute_metrics
           UpperCAmelCase			       =		self.args.eval_batch_size * self.args.world_size
           if f"{metric_key_prefix}_jit_compilation_time" in output.metrics:
               start_time += output.metrics[f"{metric_key_prefix}_jit_compilation_time"]
           output.metrics.update(
               speed_metrics(
                   lowercase   ,				lowercase   ,				num_samples=output.num_samples   ,				num_steps=math.ceil(output.num_samples / total_batch_size				)   ,				)				)
           if self.post_process_function is not None and self.compute_metrics is not None and self.args.should_save:
               # Only the main node write the results by default
               UpperCAmelCase			       =		self.post_process_function(lowercase   ,				lowercase   ,				output.predictions				)
               UpperCAmelCase			       =		self.compute_metrics(lowercase				)

               # Prefix all keys with metric_key_prefix + '_'
               for key in list(metrics.keys()				):
                   if not key.startswith(f"{metric_key_prefix}_"				):
                       UpperCAmelCase			       =		metrics.pop(lowercase				)
               metrics.update(output.metrics				)
           else:
               UpperCAmelCase			       =		output.metrics

           if self.args.should_log:
               # Only the main node log the results by default
               self.log(lowercase				)

           if self.args.tpu_metrics_debug or self.args.debug:
               # tpu-comment: Logging debug metrics for PyTorch/XLA (compile, execute times, ops, etc.)
               xm.master_print(met.metrics_report()				)

           UpperCAmelCase			       =		self.callback_handler.on_evaluate(self.args   ,				self.state   ,				self.control   ,				lowercase				)
           return metrics







       def     A      (	self						:	Optional[Any]   ,				lowercase						:	Optional[Any]   ,				lowercase						:	Tuple   ,				lowercase						:	Optional[Any]=None   ,				lowercase						:	str = "test"				):



           '''simple docstring'''

           UpperCAmelCase			       =		self.get_test_dataloader(lowercase				)

           # Temporarily disable metric computation, we will do it in the loop here.
           UpperCAmelCase			       =		self.compute_metrics
           UpperCAmelCase			       =		None
           UpperCAmelCase			       =		self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop
           UpperCAmelCase			       =		time.time()
           try:
               UpperCAmelCase			       =		eval_loop(
                   lowercase   ,				description='''Prediction'''   ,				prediction_loss_only=True if compute_metrics is None else None   ,				ignore_keys=lowercase   ,				metric_key_prefix=lowercase   ,				)
           finally:
               UpperCAmelCase			       =		compute_metrics
           UpperCAmelCase			       =		self.args.eval_batch_size * self.args.world_size
           if f"{metric_key_prefix}_jit_compilation_time" in output.metrics:
               start_time += output.metrics[f"{metric_key_prefix}_jit_compilation_time"]
           output.metrics.update(
               speed_metrics(
                   lowercase   ,				lowercase   ,				num_samples=output.num_samples   ,				num_steps=math.ceil(output.num_samples / total_batch_size				)   ,				)				)

           if self.post_process_function is None or self.compute_metrics is None:
               return output

           UpperCAmelCase			       =		self.post_process_function(lowercase   ,				lowercase   ,				output.predictions   ,				'''predict'''				)
           UpperCAmelCase			       =		self.compute_metrics(lowercase				)

           # Prefix all keys with metric_key_prefix + '_'
           for key in list(metrics.keys()				):
               if not key.startswith(f"{metric_key_prefix}_"				):
                   UpperCAmelCase			       =		metrics.pop(lowercase				)
           metrics.update(output.metrics				)
           return PredictionOutput(predictions=predictions.predictions   ,				label_ids=predictions.label_ids   ,				metrics=lowercase				)











'''simple docstring'''
a__							:							Optional[Any] =256
# Modulus to hash a string
a__							:							Dict =1_000_003
def 				lowercase__   (   __lowercase						:  str				,  __lowercase						:  str     ) ->      bool:



       """simple docstring"""






       __UpperCamelCase       =  len(__lowercase     )
       __UpperCamelCase       =  len(__lowercase     )
       if p_len > t_len:
              return False

       __UpperCamelCase       =  0
       __UpperCamelCase       =  0
       __UpperCamelCase       =  1

       # Calculating the hash of pattern and substring of text
       for i in range(__lowercase     ):
              __UpperCamelCase       =  (ord(pattern[i]     ) + p_hash * alphabet_size) % modulus
              __UpperCamelCase       =  (ord(text[i]     ) + text_hash * alphabet_size) % modulus
              if i == p_len - 1:
                     continue
              __UpperCamelCase       =  (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
              __UpperCamelCase       =  (
                  (text_hash - ord(text[i]     ) * modulus_power) * alphabet_size
                  + ord(text[i + p_len]     )
              ) % modulus
       return False
def 				lowercase__   (   ) ->      None:



       """simple docstring"""






       __UpperCamelCase       =  'abc1abc12'
       __UpperCamelCase       =  'alskfjaldsabc1abc1abc12k23adsfabcabc'
       __UpperCamelCase       =  'alskfjaldsk23adsfabcabc'
       assert rabin_karp(__lowercase				,  __lowercase     ) and not rabin_karp(__lowercase				,  __lowercase     )

       # Test 2)
       __UpperCamelCase       =  'ABABX'
       __UpperCamelCase       =  'ABABZABABYABABX'
       assert rabin_karp(__lowercase				,  __lowercase     )

       # Test 3)
       __UpperCamelCase       =  'AAAB'
       __UpperCamelCase       =  'ABAAAAAB'
       assert rabin_karp(__lowercase				,  __lowercase     )

       # Test 4)
       __UpperCamelCase       =  'abcdabcy'
       __UpperCamelCase       =  'abcxabcdabxabcdabcdabcy'
       assert rabin_karp(__lowercase				,  __lowercase     )

       # Test 5)
       __UpperCamelCase       =  'Lü'
       __UpperCamelCase       =  'Lüsai'
       assert rabin_karp(__lowercase				,  __lowercase     )
       __UpperCamelCase       =  'Lue'
       assert not rabin_karp(__lowercase				,  __lowercase     )
       print('Success.'     )


if __name__ == "__main__":
    test_rabin_karp()
