

'''simple docstring'''







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__,    UpperCAmelCase__			)     ->     str:
       A_		   = tuple((re.compile(x + """$"""			) for x in qs)			)
       for i in range(len(UpperCAmelCase__			) - len(UpperCAmelCase__			) + 1			):
              A_		   = [x.match(UpperCAmelCase__			) for x, y in zip(UpperCAmelCase__,    ks[i:]			)]
              if matches and all(UpperCAmelCase__			):
                     return True
       return False
def  UpperCAmelCase__      ( UpperCAmelCase__			)     ->     List[Any]:
       def replace(UpperCAmelCase__,    UpperCAmelCase__			):
              for rule, replacement in rules:
                     if _match(UpperCAmelCase__,    UpperCAmelCase__			):
                            return replacement
              return val

       return replace
def  UpperCAmelCase__      ( )     ->     int:
       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__			)     ->     str:
       A_		   = _get_partition_rules()
       A_		   = _replacement_rules(UpperCAmelCase__			)
       A_		   = {k: _unmatched for k in flatten_dict(UpperCAmelCase__			)}
       A_		   = {k: replace(UpperCAmelCase__,    UpperCAmelCase__			) for k, v in initd.items()}
       assert _unmatched not in result.values(), "Incomplete partition spec."
       return freeze(unflatten_dict(UpperCAmelCase__			)			)



'''simple docstring'''







import math
import sys
def  UpperCAmelCase__      ( UpperCAmelCase__			)     ->     int:
       if number != int(UpperCAmelCase__			):
              raise ValueError("""the value of input must be a natural number"""			)
       if number < 0:
              raise ValueError("""the value of input must not be a negative number"""			)
       if number == 0:
              return 1
       A_		   = [-1] * (number + 1)
       A_		   = 0
       for i in range(1,    number + 1			):
              A_		   = sys.maxsize
              A_		   = int(math.sqrt(UpperCAmelCase__			)			)
              for j in range(1,    root + 1			):
                     A_		   = 1 + answers[i - (j**2)]
                     A_		   = min(UpperCAmelCase__,    UpperCAmelCase__			)
              A_		   = answer
       return answers[number]


if __name__ == "__main__":
      import doctest

      doctest.testmod()
