


lowerCAmelCase__      		= 2_56
# Modulus to hash a string
lowerCAmelCase__      		= 1_00_00_03
def 							_UpperCAmelCase				(UpperCamelCase__     :  str				,							UpperCamelCase__     :  str						):
       _A      :       Optional[Any]				    = len(UpperCamelCase__						)
       _A      :       str				    = len(UpperCamelCase__						)
       if p_len > t_len:
              return False

       _A      :       Tuple				    = 0
       _A      :       Dict				    = 0
       _A      :       List[Any]				    = 1

       # Calculating the hash of pattern and substring of text
       for i in range(UpperCamelCase__						):
              _A      :       List[str]				    = (ord(pattern[i]						) + p_hash * alphabet_size) % modulus
              _A      :       int				    = (ord(text[i]						) + text_hash * alphabet_size) % modulus
              if i == p_len - 1:
                     continue
              _A      :       Union[str, Any]				    = (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
              _A      :       Optional[int]				    = (
                  (text_hash - ord(text[i]						) * modulus_power) * alphabet_size
                  + ord(text[i + p_len]						)
              ) % modulus
       return False
def 							_UpperCAmelCase				():
       _A      :       Tuple				    = "abc1abc12"
       _A      :       Union[str, Any]				    = "alskfjaldsabc1abc1abc12k23adsfabcabc"
       _A      :       List[str]				    = "alskfjaldsk23adsfabcabc"
       assert rabin_karp(UpperCamelCase__				,							UpperCamelCase__						) and not rabin_karp(UpperCamelCase__				,							UpperCamelCase__						)

       # Test 2)
       _A      :       List[str]				    = "ABABX"
       _A      :       Dict				    = "ABABZABABYABABX"
       assert rabin_karp(UpperCamelCase__				,							UpperCamelCase__						)

       # Test 3)
       _A      :       int				    = "AAAB"
       _A      :       Optional[int]				    = "ABAAAAAB"
       assert rabin_karp(UpperCamelCase__				,							UpperCamelCase__						)

       # Test 4)
       _A      :       Dict				    = "abcdabcy"
       _A      :       List[Any]				    = "abcxabcdabxabcdabcdabcy"
       assert rabin_karp(UpperCamelCase__				,							UpperCamelCase__						)

       # Test 5)
       _A      :       List[str]				    = "Lü"
       _A      :       List[Any]				    = "Lüsai"
       assert rabin_karp(UpperCamelCase__				,							UpperCamelCase__						)
       _A      :       Optional[int]				    = "Lue"
       assert not rabin_karp(UpperCamelCase__				,							UpperCamelCase__						)
       print("Success."						)


if __name__ == "__main__":
    test_rabin_karp()




import re

from flax.core.frozen_dict import freeze
from flax.traverse_util import flatten_dict, unflatten_dict
from jax.experimental import PartitionSpec as P


# Sentinels
lowerCAmelCase__      		= object()

# For specifying empty leaf dict `{}`
lowerCAmelCase__      		= object()
def 							_UpperCAmelCase				(UpperCamelCase__     :  str				,							UpperCamelCase__     :  Union[str, Any]						):
       _A      :       str				    = tuple((re.compile(x + "$"						) for x in qs)						)
       for i in range(len(UpperCamelCase__						) - len(UpperCamelCase__						) + 1						):
              _A      :       Tuple				    = [x.match(UpperCamelCase__						) for x, y in zip(UpperCamelCase__				,							ks[i:]						)]
              if matches and all(UpperCamelCase__						):
                     return True
       return False
def 							_UpperCAmelCase				(UpperCamelCase__     :  str						):
       def replace(UpperCamelCase__     :  List[str]				,							UpperCamelCase__     :  Optional[int]						):
              for rule, replacement in rules:
                     if _match(UpperCamelCase__				,							UpperCamelCase__						):
                            return replacement
              return val

       return replace
def 							_UpperCAmelCase				():
       return [
           # embeddings
           (("transformer", "wpe", "embedding"), P("mp"				,							UpperCamelCase__						)),
           (("transformer", "wte", "embedding"), P("mp"				,							UpperCamelCase__						)),
           # atention
           (("attention", "(q_proj|k_proj|v_proj)", "kernel"), P(UpperCamelCase__				,							"mp"						)),
           (("attention", "out_proj", "kernel"), P("mp"				,							UpperCamelCase__						)),
           (("attention", "out_proj", "bias"), None),
           # mlp
           (("mlp", "c_fc", "kernel"), P(UpperCamelCase__				,							"mp"						)),
           (("mlp", "c_fc", "bias"), P("mp"						)),
           (("mlp", "c_proj", "kernel"), P("mp"				,							UpperCamelCase__						)),
           (("mlp", "c_proj", "bias"), None),
           # layer norms
           ((r"ln_\d+", "bias"), None),
           ((r"\d+", r"ln_\d+", "scale"), None),
           (("ln_f", "bias"), None),
           (("ln_f", "scale"), None),
       ]






def 							_UpperCAmelCase				(UpperCamelCase__     :  List[str]						):
       _A      :       int				    = _get_partition_rules()
       _A      :       Optional[int]				    = _replacement_rules(UpperCamelCase__						)
       _A      :       Optional[int]				    = {k: _unmatched for k in flatten_dict(UpperCamelCase__						)}
       _A      :       List[str]				    = {k: replace(UpperCamelCase__				,							UpperCamelCase__						) for k, v in initd.items()}
       assert _unmatched not in result.values(), "Incomplete partition spec."
       return freeze(unflatten_dict(UpperCamelCase__						)						)
