



'''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   _A       (   _lowerCAmelCase  ,   _lowerCAmelCase					):



							"""simple docstring"""



							__lowercase       	=tuple((re.compile(x + '$'					) for x in qs)					)
							for i in range(len(_lowerCAmelCase					) - len(_lowerCAmelCase					) + 1					):
														__lowercase       	=[x.match(_lowerCAmelCase					) for x, y in zip(_lowerCAmelCase  ,   ks[i:]					)]
														if matches and all(_lowerCAmelCase					):
																					return True
							return False



def   _A       (   _lowerCAmelCase					):



							"""simple docstring"""



							def replace(_lowerCAmelCase  ,   _lowerCAmelCase					):
														for rule, replacement in rules:
																					if _match(_lowerCAmelCase  ,   _lowerCAmelCase					):
																												return replacement
														return val

							return replace



def   _A       (   ):



							"""simple docstring"""



							return [
							    # embeddings
							    (("transformer", "wpe", "embedding"), P('mp'  ,   _lowerCAmelCase					)),
							    (("transformer", "wte", "embedding"), P('mp'  ,   _lowerCAmelCase					)),
							    # atention
							    (("attention", "(q_proj|k_proj|v_proj)", "kernel"), P(_lowerCAmelCase  ,   'mp'					)),
							    (("attention", "out_proj", "kernel"), P('mp'  ,   _lowerCAmelCase					)),
							    (("attention", "out_proj", "bias"), None),
							    # mlp
							    (("mlp", "c_fc", "kernel"), P(_lowerCAmelCase  ,   'mp'					)),
							    (("mlp", "c_fc", "bias"), P('mp'					)),
							    (("mlp", "c_proj", "kernel"), P('mp'  ,   _lowerCAmelCase					)),
							    (("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   _A       (   _lowerCAmelCase					):



							"""simple docstring"""



							__lowercase       	=_get_partition_rules()
							__lowercase       	=_replacement_rules(_lowerCAmelCase					)
							__lowercase       	={k: _unmatched for k in flatten_dict(_lowerCAmelCase					)}
							__lowercase       	={k: replace(_lowerCAmelCase  ,   _lowerCAmelCase					) for k, v in initd.items()}
							assert _unmatched not in result.values(), "Incomplete partition spec."
							return freeze(unflatten_dict(_lowerCAmelCase					)					)








'''simple docstring'''



def   _A       (   _lowerCAmelCase					):



							"""simple docstring"""



							if not isinstance(_lowerCAmelCase  ,   _lowerCAmelCase					):
														raise TypeError('only integers accepted as input'					)
							else:
														__lowercase       	=str(abs(_lowerCAmelCase					)					)
														__lowercase       	=[list(_lowerCAmelCase					) for char in range(len(_lowerCAmelCase					)					)]
														for index in range(len(_lowerCAmelCase					)					):
																					num_transpositions[index].pop(_lowerCAmelCase					)
														return max(
														    int(''.join(list(_lowerCAmelCase					)					)					) for transposition in num_transpositions					)


if __name__ == "__main__":
		__import__("""doctest""").testmod()



